Javascript jQuery JSON 解析错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7040705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery JSON Parsing Error
提问by Nik
I have a web page that returns this JSON when called:
我有一个在调用时返回此 JSON 的网页:
[{"id":"53","desc":"Roberts"}]
I am using this jQuery to call it by AJAX:
我正在使用这个 jQuery 通过 AJAX 调用它:
$.ajax ({
url: rootPath + "data/topology/stations",
dataType: 'json',
data: { areaID: $("#lbxArea").val () },
success: function (data) {
// Use data for actions
},
error: function (jqXHR, textStatus, errorThrown) {
alert (textStatus);
alert (errorThrown);
}
});
I used Firebug to confirm that the data being returned is what I put on top. Despite that, I fall into the error
callback and first see parsererror
in an alert box, and then I see
我使用 Firebug 来确认返回的数据是我放在最上面的。尽管如此,我还是陷入了error
回调中,首先看到parsererror
了一个警告框,然后我看到了
SyntaxError: JSON.parse: expected property name or '}'
I tried having the service return
我尝试让服务返回
{"list":[{"id":"53","desc":"Roberts"}]}
but that didn't change anything.
但这并没有改变任何事情。
回答by Arthur Neves
Whats the response content-type?! Try testing this response using this:
什么是响应内容类型?!尝试使用以下方法测试此响应:
Getting the response content-type from jQuery.Post
also try not having the dataType: 'json' and check the return!
也尝试没有 dataType: 'json' 并检查返回!
回答by Fabian Vargas
Well, I've spent some time on this question, but I'll make something that will serve those who have this problem.
嗯,我已经在这个问题上花了一些时间,但我会做出一些可以为遇到这个问题的人服务的东西。
The mistake is in wanting a property accesder response coming from PHP, introducing the following message:
错误在于想要来自 PHP 的属性访问器响应,引入以下消息:
*SyntaxError: JSON. parse: expected property name or '}'*
What you should do is convert JSON response to this is to use the function JSON.parse (data); inside we pass the response variable "data". I will comment a bit the code for better understanding:
您应该做的是将 JSON 响应转换为此使用函数 JSON.parse (data); 在里面我们传递响应变量“data”。为了更好地理解,我将对代码进行一些注释:
?? success: function (data) {/ / on success ..
var json = JSON.parse (data);/ / Convert the JSON format input
console.log (JSON.parse (data));/ / explore the answer
? alert ("" + json.msj);/ / Variable Access Testing with alert
....
Everything seems fine up here, however is dimensioned present the error, then that is because the way it is performing the response from PHP.
这里的一切似乎都很好,但是尺寸显示错误,那是因为它执行来自 PHP 的响应的方式。
Here is a practical way to do it right:
这是一种正确的做法:
We use json_encode function to return the data in JSON format, within the function passed an associative array with the variables that are required, Example:
我们使用 json_encode 函数以 JSON 格式返回数据,在该函数内传递了一个关联数组,其中包含所需的变量,例如:
echo json_encode (array ('success' => true, 'msg' => 'Hello registered user!'));
After these variables acquire client side without any problem, and simply, here a nitrous-code:
在这些变量毫无问题地获取客户端之后,简单地说,这里是一个亚硝酸代码:
$. ajax ({/ / create an AJAX call ...
data: $ (this). serialize (), / / get the form data
type: $ (this). attr ('method'), / / GET or POST
url: $ (this). attr ('action'), / / the file to call
cache: false,
success: function (data) {/ / on success ..
var json = JSON.parse (data);
$ ('# created'). html (json.msj) / / update the DIV
I hope will be helpful ... any questions feel free to ask ...
我希望会有所帮助...任何问题随时问...
回答by 321X
You can install a Firefox add-on "JSONView". Maybe this gives you more information about the JSON string.
您可以安装 Firefox 附加组件“JSONView”。也许这会为您提供有关 JSON 字符串的更多信息。
If you don't see anything (special JSON markup), you probaby miss a JSON header.
如果你没有看到任何东西(特殊的 JSON 标记),你可能错过了一个 JSON 标头。
Edit:Firebug 1.4+ should show a JSON tab at a request.
编辑:Firebug 1.4+ 应在请求时显示 JSON 选项卡。