简体   繁体   English

如何从php ajax请求和显示中拆分接收到的数据

[英]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. 我正在尝试从另一个PHP文件中接收两个变量(例如var1是name,而var2是city)。

Using jquery ajax requests, its showing whole "data" in one string ie data : name - city. 使用jquery ajax请求,它在一个字符串中显示整个“数据”,即data:name-city。

But i want to split both name,city separately and assign to different li elements in html. 但是我想分别拆分名称和城市,并分配给HTML中的不同li元素。

How to do this? 这个怎么做? Please help.. 请帮忙..

Note: Names and cities i am taking from db in php. 注意:我从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: 使用以下方法使PHP以JSON格式发送数据:

$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: 现在,您可以通过执行以下操作在Javascript中接收该数据:

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

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

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