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

I am sending a request to my CI program from a remote javascript program running in a browser. The call on the javascript client is 
Code:
           var xhr = new XMLHttpRequest();                
           xhr.onerror = function() { alert('error'); };
        xhr.addEventListener ("load", reqListener);
        xhr.open('GET', 'http://localhost/Subit_backend/register', true);  //used .htdaccess in apache server to modify the injection of index.php as per CI forum 3/17/18
           myArray["email"]=jsonResponse["email"];
        var json = JSON.stringify(myArray);
        document.onreadystatechange = function () {
        if (xhr.readyState===1)
            {
            xhr.send(json);
            }
           
          }
On the server side I have 

PHP Code:
  public function register()
 
   {
 
   if (isset($_GET["email"])) {
 
   
        
}
 
   
The controller fires and I have a breakpoint on the if (isset()) line. But isset never seems to catch the $_GET["email"]; it always fails. I am actually sending over three data items from the client in myArray, the email address, the Latitude and the longitude. How can I see what buffer is being received by the controller? Is there anything obvious I am missing?
proof that an old dog can learn new tricks
Reply
#2

If i read this documentation correctly the parameter for send is ignored for GET requests. You probably need to do a POST request.
Reply
#3

Thanks Dave. The reason I am using GET is that I need to get data back from the server. The Browser sends the email address of the user and the server sends back a list of data for that user which I use to populate the popup in the browser. My understanding is that Post is a one way path from the browser to the server.
proof that an old dog can learn new tricks
Reply
#4

(03-21-2018, 08:55 AM)dave friend Wrote: If i read this documentation correctly the parameter for send is ignored for GET requests. You probably need to do a POST request.

That's indeed correct. Or add the variable into the url, to get GET working.

(03-21-2018, 09:37 AM)richb201 Wrote: My understanding is that Post is a one way path from the browser to the server.

No, you can send back information with any method.

Getting the data:
https://www.codeigniter.com/user_guide/l...put-stream

Send back JSON.
https://www.codeigniter.com/user_guide/l...ntent_type
Reply
#5

(This post was last modified: 03-21-2018, 02:27 PM by dave friend.)

You can send data back from a POST too and it is done identically to the way you would respond to a GET. It's only AJAX after all.

You'll want to add handling to the JavaScript.

Code:
document.onreadystatechange = function () {
   if (xhr.readyState === 1)
  {
       xhr.send(json);
   }
   if (xhr.readyState == 4) // DONE
  {
       doSomethingWith(xhr.response);
   }

You can also use event listeners to handle readyState changes. Here's one for DONE a.k.a. xhr.readyState == 4

Code:
xhr.addEventListener("loadend", callbackFn, false);

Where "callbackFn" is a function to handle the response. It can be an in-line anonymous function too.

And if you really, really, really insist on using get the way to pass data is to construct your url complete with a query string.

Code:
var url = 'http://localhost/Subit_backend/register' + '?email=' + jsonResponse["email"];  // if jsonResponse["email"] is a string
xhr.open('GET', url, true);
Reply
#6

The document.onreadystatechange is not firing, although I do see the xhr.readyState go from 0 to 1. I am in a background script. Will that affect the use of statechange?

document.onreadystatechange = function ()
proof that an old dog can learn new tricks
Reply
#7

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

I managed to see the message being sent in the network debugger. It is:
http://localhost/Subit_backend/register%....com%22%7D

The status (from the network tab in the chrome debugger) is (pending). Is this because it was never recvd by Subit_backend?



   class Subit_backend extends CI_Controller
   {

       public function __construct()
       {
       parent::__construct();

       $this->load->database();
       $this->load->helper('url', 'input' );
         }


       public function register()
      {
      if (isset($_GET["email"])) {
          $this->db->db_select("employees");
        }
      }
   }
proof that an old dog can learn new tricks
Reply
#8

(This post was last modified: 03-25-2018, 03:32 AM by jreklund.)

Just send it with POST instead. It's so much easier and your url don't get bloated.

You are adding the following in the url:
Code:
{"lat":41.058304899999996,"long":-74.0711244,"email":"[email protected]"}

But there are no ?email= so it dosen't know you want to pick it up with $_GET.
http://localhost/Subit_backend/register?email=%7B%22lat%22:41.058304899999996,%22long%22:-74.0711244,%22email%22:%[email protected]%22%7D
Reply
#9

I didn't realize that there needed to be an "=" to use $_GET in php. But as I was saying I am not even getting to the $_GET["email"] line in my controller.  I am using JSON.stringify of my array which contains "lat", x, "long", y, "email", email_address. Perhaps that is the mistake? I can surely use POST instead of GET if nothing else needs to change in the xhr.open().  I took a look at the network messages and it seems that the message from the client is OK (except for the lack of "=" !) but it is stuck in status = pending which I think is a problem with the server and the way I have set up the debugger in phpStorm.  I put in a support request from them but it could easily be a week before they respond. In the meantime I will try POST to see if it changes things. I also tried switching to just sending "email=". This is the header using =

  1. Request URL:
    http://localhost/Subit_backend/[email protected]

  2. Referrer Policy:
    no-referrer-when-downgrade
But the communication is stuck in "pending". But when I turn off the "Listen for PHP debug Connections" in phpStorm, status = 200. But I can't debug. I have a document called Simultaneous debugging sessions with phpStorm printed out that I will read over this morning. It talks about debugging both the client and the server under phpStorm which I really don't want to do (since this is an extension), but who knows?
proof that an old dog can learn new tricks
Reply
#10

(This post was last modified: 03-25-2018, 06:20 AM by jreklund. Edit Reason: Added GET )

If you wan't to send pure Json with XMLHttpRequest*, this is how you do it.
You will need to add readyState etc yourself.

* With POST. GET are just there so you can look at something that works. But that's not recommended.
Code:
<html>
  <head>
    <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.
    };
    
    xhr.send(JSON.stringify(data));

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

    xhr.onload = function () {
      // Request finished. Do processing here.
    };
    
    xhr.send(null);

    </script>
  </head>
  <body>
  </body>
</html>

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

class 
Json extends CI_Controller {
    
    public function 
index()
    {
        
$this->load->view('json');
    }

    public function 
register()
    {
        if( 
$this->input->is_ajax_request() )
        {
            
$json $this->input->raw_input_stream;
            
$json json_decode($json);
            
var_dump($json);
        }
    }
    
    public function 
register_get()
    {
        if( 
$this->input->is_ajax_request() )
        {
            
$json $this->input->get('email'); // or $_GET['email'];
            
$json json_decode($json);
            
var_dump($json);
        }
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB