简体   繁体   English

使用JQuery从具有相同POST参数的相同PHP文件获得不同的AJAX XML结果

[英]Different AJAX XML results from same PHP file with same POST parameters using JQuery

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). 我正在使用JQuery .ajax方法来获取XML中的简单PHP查询的结果(我在该项目中已经完成了很多次而没有问题)。 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. 但是,在Firebug控制台中查看结果时,我从主项目中收到的XML结果与期望的结果不同(通过简单查看PHP文件进行测试)。 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. 我还通过创建一个新的PHP文件(它的唯一功能是运行ajax调用)来再次测试了此结果,结果与预期的一样正确。

[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.. 为了进行测试,我通过删除所有POST数据和成功的回调函数简化了过程,但仍然得到不同的结果。

Here are the code snippets: 以下是代码片段:

The JQuery ajax call (used in both the test file and within my project is): JQuery ajax调用(在测试文件和项目中均使用):

    $.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: PHP代码是:

$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): 测试文件测试PHP文件的Firebug标头(返回正确的结果是):

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: 主项目来自主项目的ajax调用的Firebug标头是:

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. 因此由于某种原因,当从主项目中调用时,内部XML节点将从结果中省略。

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! 任何帮助是极大的赞赏!

My guess is you should be using mysql_fetch_assoc instead of mysql_fetch_array. 我的猜测是您应该使用mysql_fetch_assoc而不是mysql_fetch_array。 mysql_fetch_array will give you an array structure with numeric indexes. mysql_fetch_array将为您提供带有数字索引的数组结构。 Where mysql_fetch_assoc will give you the $array[column_name] like you are looking for. mysql_fetch_assoc将在其中提供您想要的$ array [column_name]。

I think that it's a mysql connection issue; 我认为这是一个mysql连接问题; the select is not running anyhow. 选择仍然无法运行。 To debug, add an echo "hello" to while{} , and see if it shows up at all or not :) 要进行调试,请在while {}中添加回声“ hello”,看看它是否完全显示出来:)

In firebug, right click on the AJAX call, then select "Open in new tab." 在Firebug中,右键单击AJAX调用,然后选择“在新标签页中打开”。 Firefox should format the XML nicely for you (if you are doing header("Content-Type: application/xml") like you should). Firefox应该为您很好地设置XML格式(如果您像应该那样做header("Content-Type: application/xml") )。 Look at the request it is sending (in the location box). 查看它正在发送的请求(在位置框中)。 Reload it a few times. 重新加载几次。 Print the data in the PHP file that you think should be set. 在您认为应该设置的PHP文件中打印数据。

I'm sorry I can't help more but it sounds like some silly mistake somwhere. 很抱歉,我无能为力,但这听起来像是一个愚蠢的错误。

Alternatively, you could just build the data up as an array, and then use JSON (my preferred method). 另外,您可以将数据构建为数组,然后使用JSON(我的首选方法)。

On more thing, you should NEVER do parameter injection into SQL. 另外,永远不要将参数注入SQL。 Bery very dangerous. 贝里非常危险。 Use something like PDO. 使用类似PDO的东西。

$userID='11';
$PDO = new PDO("mysql:dbname=mydb", "uname", "pword");

$stmt = $PDO->prepare("SELECT ID, name, pic_square FROM users WHERE ID = :userID");
$stmt->execute(array("userID" => $userID)) or die (print_r($PDO->errorInfo(), TRUE));

$output = array();
while($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
    $output = $row;
}

header('Content-Type: application/json; charset=ISO-8859-1');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

print json_encode($output);

Seems like you're not getting data from the db. 似乎您没有从数据库获取数据。 What happends if you just do this 如果您这样做,会发生什么

echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
echo "<Friends>";
echo "<Test>Hello</Test>";
echo "</Friends>";

instead? 代替? If it really is a cache problem, then adding a random param to the URL should force the browser to update. 如果确实是缓存问题,则在URL中添加随机参数会强制浏览器进行更新。 Ie does this should work: 即这样做应该工作:

$.ajax({
    url:"./lib/ajax_friends.php?random987398792374",
...

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM