gpt4 book ai didi

php - 我的 JSON 有什么问题

转载 作者:行者123 更新时间:2023-11-28 13:41:59 25 4
gpt4 key购买 nike

我正在使用 jquery 函数 $.getJson。它正在发送我想要的数据,并且生成 JSON 的 PHP 脚本正在正常工作。但是,我遇到的问题是从这里发生的。

在我的 $.getJSON 代码中,我希望它在成功时简单地记录结果,如果出现错误则记录为错误...你猜对了...我收到错误...

这是我传递给它的 JSON 代码,它产生了错误,但我看不出它出了什么问题......

  {
"results":{
"city":[
"metric":"visits",
"dimension":"city",
"data":[["London","5"],["Glasgow","3"],["(not set)","1"],["Aberdeen","1"],["Antalya","1"]]
],

"browser":[
"metric":"visits",
"dimension":"browser",
"data":[["Internet Explorer","11"],["Chrome","9"],["Firefox","3"],["Safari","3"],["Android Browser","2"]]
],

"screenResolution":[
"metric":"visits",
"dimension":"screenResolution",
"data":[["1280x800","5"],["1680x1050","4"],["1024x768","3"],["1366x768","3"],["1280x1024","2"]]
]
}
}

更新以显示代码

这是我用于获取结果的代码

$.ajax({

//DEFINE URL being called by Ajax
url:'./getAnalytics.php',
dataType: "json",
data:{dateToSearch:dateToSearch},
success:function(resultsToView){

console.log(resultsToView);


},error:function(resultsToView){
console.log("Error: " + resultsToView);
}
});

控制台显示: Result of resultsToView

当我得到这个信息时,调用的是错误函数而不是成功函数。请帮忙。

最佳答案

您的JSON 无效。方括号表示一个数组(列表),其中的项没有键。改变

[
"metric":"visits",
"dimension":"…",

]

到带有大括号的对象(映射)语法:

{
"metric":"visits",
"dimension":"…",

}
<小时/>

此外,错误回调具有签名 Function(jqXHR xhr, String textStatus, String errorThrown)。您正在记录 xhr 对象的字符串化,这没有帮助。更好地使用

…,
error: function(xhr, status, error) {
console.log("Error: "+error+" ("+status+")", xhr);
}

关于php - 我的 JSON 有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17276589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com