简体   繁体   English

解析ajax响应文本

[英]parse ajax response text

I want to update the web page based on ajax response received from a php script. 我想根据从php脚本收到的ajax响应来更新网页。

code inside php page : php页面内的代码:

// based on the logic either of the following three will be returned using ajax repsonse. //根据以下三个逻辑之一,将使用ajax repsonse返回。

echo "<div align='center>Yahoo</div>";

echo "<div align='center>Rediff</div>";

echo "<div align='center>Google</div>";

The page which calls the ajax and receive the repsonse needs to perform some actions based on the returned response text. 调用ajax并接收回复的页面需要根据返回的响应文本执行一些操作。

Like , when the reponse includes "Yahoo" i need to execute some javascript functions.... when the reponse includes "Rediff" i need to perform some other javascript functions.... 像一样,当响应包括“ Yahoo”时,我需要执行一些javascript函数....当响应包括“ Rediff”时,我需要执行其他一些javascript函数。

Currently i am using the javascript .indexOf function to search for "Yahoo" or "Rediff" in the ajax response and based on the return status of .indexOf() i am calling the functions i wish to execute..... 目前,我正在使用javascript .indexOf function在ajax响应中搜索“ Yahoo”或“ Rediff”,并且根据.indexOf()的返回状态,我正在调用要执行的函数.....

I feel i am not doing it in the right way ..... so thats why this question !!! 我觉得我没有以正确的方式做.....所以这就是为什么这个问题!

Can JSON be used in this case ??? 在这种情况下可以使用JSON吗? [ just a tech thought :-) ] [只是一个技术思想:-)

i use json quite a lot for ajax apps, you could simply have a response such as 我对ajax应用程序使用json的次数很多,您可能会得到如下响应:

{
    "response":"rediff",
}

then eval that, and get the .response value. 然后进行评估,并获得.response值。 works perfectly for me 最适合我

the response from the php script would be the same as above, then (the way i interpret json) use php脚本的响应将与上面相同,然后(我解释json的方式)使用

var resp = eval("("+req.responseText+")");
resp = resp.response;

resp will now return "rediff" resp现在将返回“ rediff”

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

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