I'm using the JQuery .ajax method to get the results of a simple PHP query in XML (which I've done various times within this project without a problem). However, the XML result that I receive from within my main project is different to what I expect (as tested by simply viewing the PHP file) when viewing the results in Firebug's console. I also tested this again by creating a new PHP file whose sole function is to run the ajax call and the results are correct, as expected.
[Update]: I just tested this out again, and I received the correct results from within my project, but when I refresh the page, I get the incorrect results again.
For the sake of testing, I simplified the process by removing all POST data and the success callback function, but I still get different results..
Here are the code snippets:
The JQuery ajax call (used in both the test file and within my project is):
$.ajax({
url:"./lib/ajax_friends.php",
type:"POST",
// data:{action : "getFriends", userID: userID} ,
dataType: 'xml',
sync:false,
error:function(request){alert("error")},
success:function(theXML){
}
})
The PHP code is:
$userID='11';
$sql = "SELECT ID, name, pic_square FROM users WHERE ID = $userID";
$result = mysql_query($sql) or die(mysql_error());
$xml = "";
while($array = mysql_fetch_array($result)) {
$ID = $array['ID'];
$Name = $array['name'];
$pic_square = $array['pic_square'];
$xml .= "<Friend>";
$xml .= "<ID>$ID</ID>";
$xml .= "<Name>$Name</Name>";
$xml .= "<Pic>$pic_square</Pic>";
$xml .= "</Friend>";
}
header('Content-Type: application/xml; charset=ISO-8859-1');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
echo "<Friends>";
echo $xml;
echo "</Friends>";
Note: I tried changing the headers to force a full refresh, but it still didn't help
Test File The headers from Firebug of the test PHP file (which returns the correct results are):
Response Headers
Date
Fri, 08 May 2009 18:53:34 GMT
Server
Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7l DAV/2 PHP/5.2.6
X-Powered-By
PHP/5.2.6
Cache-Control
no-cache, must-revalidate
Expires
Mon, 26 Jul 1997 05:00:00 GMT
Content-Length
200
Keep-Alive
timeout=5, max=98
Connection
Keep-Alive
Content-Type
application/xml; charset=ISO-8859-1
Request Headers
Host
localhost
User-Agent
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept
application/xml, text/xml, */*
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
300
Connection
keep-alive
X-Requested-With
XMLHttpRequest
Referer
http://localhost/~Seeff/testajax.php
Cookie
a7c768c2549daf4a7f69b9916bab5a38=4555bf36cf1b308f19a12f8da6944b80; a7c768c2549daf4a7f69b9916bab5a38_user
=507514167; a7c768c2549daf4a7f69b9916bab5a38_ss=kKgqsUlOrFArzo9Nrv2Zyg__; a7c768c2549daf4a7f69b9916bab5a38_session_key
=3.qPXakpbNIIX_bvndm_5gnA__.86400.1241895600-507514167; a7c768c2549daf4a7f69b9916bab5a38_expires=1241895600
; fbsetting_a7c768c2549daf4a7f69b9916bab5a38=%7B%22connectState%22%3A1%2C%22oneLineStorySetting%22%3A1
%2C%22shortStorySetting%22%3A1%2C%22inFacebook%22%3Afalse%7D
And the Response is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Friends><Friend><ID>12</ID><Name>XXX
</Name><Pic>http://something</Pic></Friend></Friends>
Main Project The Firebug headers from the ajax call from my main project are:
Response Headers
Date
Fri, 08 May 2009 18:53:41 GMT
Server
Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7l DAV/2 PHP/5.2.6
X-Powered-By
PHP/5.2.6
Cache-Control
no-cache, must-revalidate
Expires
Mon, 26 Jul 1997 05:00:00 GMT
Content-Length
74
Keep-Alive
timeout=5, max=98
Connection
Keep-Alive
Content-Type
application/xml; charset=ISO-8859-1
Request Headers
Host
localhost
User-Agent
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept
application/xml, text/xml, */*
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
300
Connection
keep-alive
X-Requested-With
XMLHttpRequest
Referer
http://localhost/~Seeff/
Cookie
a7c768c2549daf4a7f69b9916bab5a38=4555bf36cf1b308f19a12f8da6944b80; a7c768c2549daf4a7f69b9916bab5a38_user
=507514167; a7c768c2549daf4a7f69b9916bab5a38_ss=kKgqsUlOrFArzo9Nrv2Zyg__; a7c768c2549daf4a7f69b9916bab5a38_session_key
=3.qPXakpbNIIX_bvndm_5gnA__.86400.1241895600-507514167; a7c768c2549daf4a7f69b9916bab5a38_expires=124
1895600
And the response is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Friends></Friends>
So for some reason, the inner XML node is being omitted from the results when called from the main project.
I'm guessing that there's something in my main project that must be interfering in some way, but I just can't figure it out. Any help is greatly appreciated!