好吧,我使用 Json 将一些数据从 PHP 发送到 Ajax(完美运行),但我的问题在于为什么我想拆分该响应。
我如何使用例如提醒每个元素?
$.ajax({
url:"myHandler.php",
type:"POST",
data:{function:"rxz", xLast:xx, yLast:yy},
dataType:'json',
success:function(data){
alert(data); //How do I alert EVERY single data element on its own?
},
error:function(data) { console.log(data); }
});
PHP 的内容是这样发送的
print json_encode(array($json_array,$from,$to));
它基本上发送一个数组并包含 From 和 To 内部
最佳答案
为什么不重构 PHP 以返回正确的数组。像这样的事情很容易处理:
print json_encode([
'data' => $json_array,
'from => $from,
'to' => $to
]);
然后您可以在 jQuery 中对其进行迭代:
jQuery.each(data.data, function(i, element) {
alert(element);
});
<小时/>
您甚至可以使用“正确”的做法并在 json 中返回状态代码 ([....'status' => true/false ....]
) 然后您就可以正确显示最终结果。
关于javascript - 在 Ajax 中从 PHP 中拆分数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40776853/