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.
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 Headers:
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
Response 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 the PHP session ID is being set, but the response tab is empty.