我一直在使用$.getJSON
来验证脚本是否已正确完成其工作,使用exit("{'status':true}")
(或false)但现在我需要脚本返回值(或数组) 。我看到我无法使用exit('{ "status": $myarray}');
。我可以用什么呢?我是php的新手 - 有可能做return '{ "status": $myarray}';
之类的东西吗?
提前致谢
答案 0 :(得分:5)
答案 1 :(得分:2)
在你的php中,使用json_encode
:
<?php
header('Content-Type: application/json');
echo json_encode($myarray);
// or, `exit(json_encode($myarray))` if global scope (see *REF)
?>
并且在你的jQuery中通常使用getJSON
:
$.getJSON("test.php",function(result){
...
});
答案 2 :(得分:1)
`$jsonString = 'your json string'`
$jsonArray = json_encode($jsonString);
return $jsonArray
答案 3 :(得分:1)
如果你想在json中返回数据,就像数组或单个数据一样 试试这段代码
在php文件中写这样的
$value = 'welcome';
echo json_encode($value);exit;
or
$value = array("Saab","Volvo","BMW","Toyota");
print_r(json_encode($value));exit;
答案 4 :(得分:0)
我会发布我做的方式,现在看起来很简单。谢谢你的回答
$json_array=array('status'=>$row);
exit(json_encode($json_array));
AJAX电话:
$.getJSON( "savefunctions/getVideos.php", function(response) {
if( response.status ) alert(response.status);
});