Controller is firing but I can't get the $_GET |
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(); PHP Code: public function register()
proof that an old dog can learn new tricks
If i read this documentation correctly the parameter for send is ignored for GET requests. You probably need to do a POST request.
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
(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
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 () { 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
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
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
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
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 =
proof that an old dog can learn new tricks
03-25-2018, 06:08 AM
(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> PHP Code: <?php |
Welcome Guest, Not a member yet? Register Sign In |