Why is my $ .getJSON response data null?

I have a very simple setup and I can’t work. I have a simple PHP page that only starts a session and should output the status and session ID in JSON. However, when the ajax call is returned, the data is always zero. I am using Firebug and I see an ajax function calling my callback.

Here's the PHP page on the server:

<?php try { if(!session_start()) { throw new Exception("unable to start session"); } echo json_encode(array( "status" => "success", "session_id" => session_id() )); } catch(Exception $e) { echo json_encode(array( "status" => "fail", "error" => $e->getMessage() )); } ?> 

It works fine and outputs something like this:

{"status": "success", "session_id": "i3cdogb9jgd6oudar104qfuih1"}

The HTML page is just as simple:

 <html> <head> <script src="jquery-1.4.1.min.js" type="text/javascript"></script> <title>getJSON example</title> <script type="text/javascript"> $(document).ready(function() { $.ajax({ url: "http://webserver/init.php", dataType: 'json', success: function(json) { if(json.status === "success"){ $("#session_key").val(json.session_id); } } }); }); </script> </head> <body> <input type="hidden" id="session_key"/> </body> </html> 

Additional Information

Request Header:

 GET /init.php HTTP/1.1 Host: ir-6483 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Origin: null 

Answer headers:

 HTTP/1.1 200 OK Date: Mon, 01 Feb 2010 16:50:11 GMT Server: Apache/2.2.14 (Win32) PHP/5.2.12 X-Powered-By: PHP/5.2.12 Set-Cookie: PHPSESSID=i033rd4618o18h3p5kenspcc35; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-Length: 101 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html 

So you can see that the PHP session id is set, but the response tab is empty.

+4
source share
3 answers

I think I found the problem (go to bold fonts for a short answer).

I tried the example on my machine by creating a .html file on the desktop and uploading a PHP fragment to my dev server on the local network. This gave me the same results as you, also with Firefox (v3.5.7).

I'm not a PHP guy (more Python / Django), but I know some basic PHP, and your code looked great. I confirmed this with a lot of PHP / JSON / Ajax searches. I tried every option asked in the consultation forums, including various header cache options in PHP and various JQuery methods, including changing some cache parameters, as well as various different options. No difference.

I completely simplified PHP to literally just return a JSON array with one element without any other conventions or control structures - without joy.

In the end, I despaired and tried the same file in IE 8. It will work! Tried this in Safari - it worked!

Then I got paranoid and thought there might be some problems with a sticky session in Firefox or some other weird thing, so I completely cleared the Firefox history and restarted the browser. No difference.

This, at least, gave me a different look at what I could see on Google if others had the same problem. Indeed, they were.

It turns out that if you use the HTML file containing Ajax on your local computer, that is, with the prefix "file: //", Firefox for some reason will not make the correct Ajax calls. I copied the file to my dev server in the same place as the PHP file (but still used the full address as the Ajax URL) and of course it will work!

I don’t quite understand what Firefox does differently with really local files and Ajax, but it glows from the outside and I can’t research right now. I looked at the security settings, but nothing obvious jumped at me. I would be very interested to know how this fits into the setup of your development environment. I will investigate later if I have a chance and update this answer.

I would not be surprised if there is some option in "about: config" that may contain an answer.

+4
source

Due to being flogged you don't seem to include jQuery.js in your HEAD document:

 <script type="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> 

EDIT:

I would try setting the Content-Type header before output from the server, just in case:

 header('Content-type: application/json'); 
+1
source

I copied the HTML and Javascript from your example, changing only the init.php call to just “init.php” (fully accepting the same server and directory), and the example worked fine. There is nothing wrong with the published code, which means you need to double-check the Firebug net tab to see which request is being executed and what is the answer. Also change the input type from hidden to text until you are sure that everything works. If you use "View Source" to search for the resulting value, you will not see it because the value was not there when the page was loaded. If you have the Firefox plug-in installed for web developers , you can "view the generated source", which will show the AJAX results in your hidden variable.

+1
source

Source: https://habr.com/ru/post/1299963/


All Articles