Welcome Guest, Not a member yet? Register   Sign In
Controller is firing but I can't get the $_GET
#11

(This post was last modified: 03-25-2018, 08:54 AM by richb201.)

Thanks. I don't have a view on the server (for Subit_backend) since there is no UI. Am I required to load a view?
proof that an old dog can learn new tricks
Reply
#12

(03-25-2018, 08:54 AM)richb201 Wrote: Thanks. I don't have a view on the server (for Subit_backend) since there is no UI. Am I required to load a view?

No, I just needed to run my JavaScript somewhere.
Reply
#13

(This post was last modified: 03-25-2018, 09:43 AM by richb201.)

I changed over to POST and added
   xhr.setRequestHeader("Content-type", "application/json");
   xhr.setRequestHeader("X-Requested-With",'xmlhttprequest');

Now, I am getting this error:

[Sun Mar 25 11:57:47.099508 2018] [:error] [pid 13272:tid 1652] [client ::1:56672] PHP Deprecated:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

Any ideas?

I am also getting:

background.js:85 POST http://localhost/Subit_backend/register%....com%22%7D 400 (Bad Request)
proof that an old dog can learn new tricks
Reply
#14

(This post was last modified: 03-25-2018, 03:20 PM by richb201.)

Here are the headers. Maybe you can see something obvious? BTW, when I send just the email, it seems to give me a status 200. So there is something wrong with the payload?
Request Headers:
POST /Subit_backend/register%7B%22lat%22:41.058304899999996,%22long%22:-74.0711244,%22email%22:%[email protected]%22%7D HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 74
Pragma: no-cache
Cache-Control: no-cache
Origin: chrome-extension://bdjgnodlhfmhghjhbkkkaaammfocdpib
X-Requested-With: xmlhttprequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
Content-type: application/json
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: ciNav=no; XDEBUG_SESSION=PHPSTORM; cisession=t387m0rtvof1rmpoell1m3uakebg97l9

Request Payload:
{lat: 41.058304899999996, long: -74.0711244, email: "[email protected]"}
email
:
"[email protected]"
lat
:
41.058304899999996
long
:
-74.0711244

Response Headers:

HTTP/1.1 400 Bad Request
Date: Sun, 25 Mar 2018 22:03:59 GMT
Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28
X-Powered-By: PHP/5.6.28
Content-Length: 1134
Connection: close
Content-Type: text/html; charset=UTF-8
proof that an old dog can learn new tricks
Reply
#15

You are still trying to send it in the url. Please check my code again and update accordingly.
Reply
#16

(This post was last modified: 03-26-2018, 10:47 PM by richb201.)

Hooray!I finally got the message from the client back to the server with POST! Thank you! Back on the client, you have:

xhr.onload = function () { //response will go here
// Request finished. Do processing here.
};

question: 1) once I build the response on the server, how do I send it back to the client? 2) I realize the onload will fire, but how do I get access to the buffer response on the client?
proof that an old dog can learn new tricks
Reply
#17

(This post was last modified: 03-27-2018, 08:34 AM by jreklund.)

Here's how you do it.

References:
https://developer.mozilla.org/en-US/docs...ng_Started
https://zqzhang.github.io/blog/2016/04/1...-ajax.html
https://www.w3schools.com/xml/ajax_xmlht...create.asp
https://www.w3schools.com/js/js_json_parse.asp
https://www.codeigniter.com/user_guide/l...ntent_type

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Json extends CI_Controller {
    
    public function 
register()
    {
        if( 
$this->input->is_ajax_request() )
        {
            
$json $this->input->raw_input_stream;
            
$json json_decode($json);
            
/**
            $json now looks like
                stdClass Object
                (
                    [lat] => 41.058304899999996
                    [long] => -74.0711244
                    [email] => [email protected]
                )
                
            Access it like
            echo $json->lat;
            **/
            
            // Process json...
            
            // Data to send back
            
$data = array('success' => 'true');
            
            
// Send back json
            
$this->output
                    
->set_content_type('application/json')
                    ->
set_output(json_encode($data));
        }
    }


Code:
<script type="text/javascript">
    var data = {"lat":41.058304899999996,"long":-74.0711244,"email":"[email protected]"};
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/json/register', true);
    
    //Send the proper header information along with the request
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.setRequestHeader("X-Requested-With",'xmlhttprequest');

    xhr.onload = function () {
        // Request finished. Do processing here.
        if(xhr.status === 200) {
            var jsonResponse = JSON.parse(xhr.responseText);
            // Log the whole json object
            console.log(jsonResponse);
            // Access 'success' from the object
            console.log(jsonResponse.success);
        }
    };
    
    xhr.send(JSON.stringify(data));
    </script>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB