简体   繁体   中英

parse ajax response text

I want to update the web page based on ajax response received from a php script.

code inside php page :

// based on the logic either of the following three will be returned using 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.

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....

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.....

I feel i am not doing it in the right way ..... so thats why this question !!!

Can JSON be used in this case ??? [ just a tech thought :-) ]

i use json quite a lot for ajax apps, you could simply have a response such as

{
    "response":"rediff",
}

then eval that, and get the .response value. works perfectly for me

the response from the php script would be the same as above, then (the way i interpret json) use

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

resp will now return "rediff"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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