Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my $.getJSON response data null?
    text
    copied!<p>I have a very simple setup that I can't seem to get working. I have a simple PHP page that just starts a session and is supposed to output the status and the session id in JSON. However, when the ajax call returns, the data is always null. I'm using Firebug, and I can see the ajax function calling my callback. </p> <p>Here's the PHP page on the server:</p> <pre><code>&lt;?php try { if(!session_start()) { throw new Exception("unable to start session"); } echo json_encode(array( "status" =&gt; "success", "session_id" =&gt; session_id() )); } catch(Exception $e) { echo json_encode(array( "status" =&gt; "fail", "error" =&gt; $e-&gt;getMessage() )); } ?&gt; </code></pre> <p>It works fine and outputs something like this:</p> <blockquote> <p>{"status":"success","session_id":"i3cdogb9jgd6oudar104qfuih1"}</p> </blockquote> <p>The HTML page is just as simple:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="jquery-1.4.1.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;title&gt;getJSON example&lt;/title&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $.ajax({ url: "http://webserver/init.php", dataType: 'json', success: function(json) { if(json.status === "success"){ $("#session_key").val(json.session_id); } } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="hidden" id="session_key"/&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>Additional Information</h2> <p>Request Headers:</p> <pre><code>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 </code></pre> <p>Response Headers:</p> <pre><code>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 </code></pre> <p>So, you can see the PHP session ID is being set, but the response tab is empty. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload