简体   繁体   中英

How to split received data from php ajax request and display

I am trying to receive two variables(say var1 is name and var2 is city) from another php file.

Using jquery ajax requests, its showing whole "data" in one string ie data : name - city.

But i want to split both name,city separately and assign to different li elements in html.

How to do this? Please help..

Note: Names and cities i am taking from db in php. So multiples names and cities will be there. I think find alone cant be used.

Make the PHP send your data in JSON by using something like:

$myData = ["name" => "foo", "city" => "bar"];
$dataToSend = json_encode($myData);
echo $dataToSend;
die();

You can now receive that data in your Javascript by doing something like:

$.getJSON("myphp.php", function (data) {
    console.log(data);
});

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