我知道您可以使用 getJSON 从 PHP 获取 JSON 数组。但我不能让它返回数组,所以我可以保存它。
有没有办法做到这一点?
例如,如果我创建了一个名为 getJSONInfo() 的函数,并且我希望它从 PHP 返回 JSON 数组。
edwardmp 的回答示例:
$.getJSON("http://site.com/file.php?id=2", function(json){
var newArray = json;
});
var username = newArray.username;
最佳答案
看起来您希望此代码异步工作:
var newArray = $.ajax({
async=true,
dataType: "json",
url: "http://site.com/file.php?id=2"
});
// this line will only be reached AFTER the HTTP request has completed
alert(newArray.username);
但是(或者)我真的建议处理所有在回调中使用 myArray.username 的代码。这就是 AJAX 最终的工作方式,否则就是 JAX!这看起来有点像这样:
function haz_json(newArray) {
alert(newArray.username);
}
$.ajax({
dataType: "json",
url: "http://site.com/file.php?id=2",
success: haz_json
});
关于php - 从php获取json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16989731/