为什么我无法处理JSON响应?
问题描述:
我找回一些JSON,并需要将其显示在我的页面上,但它似乎不会显示!为什么我无法处理JSON响应?
有人可以请看看,看看你是否能看到我要去哪里错了?
代码:
$.ajax({ type: "GET",
url: "URL GOES HERE - Cant give for obvious reasons :)",
dataType: "jsonp",
success: function (response) {
var result = response.d;
$.each(result, function (index, res) {
$('#questions').val(res.q);
});
},
error: function (msg) {
}
});
JSON:
{
"d": [
{
"id": "1002 ",
"q": "What region is Auchentoshan whisky made in",
"a1": "Highlands",
"a2": "Speyside",
"a3": "Lowlands"
},
{
"id": "1042 ",
"q": "Chase’s award winning vodka is made from...",
"a1": "Grapes",
"a2": "Rye",
"a3": "Potatoes"
}
]
}
答
随着dataType
的jsonp
响应预期是一个JSON斑点包裹在一个函数调用。由于您只返回JSON,因此您应该使用json
的dataType
。
我调整了您的示例以使用JSFiddle echo API。在first version中,我使用了代码中使用的jsonp
的dataType
。正如你看到,如果你访问该链接,它不起作用。在edited version我用dataType
json
它工作得很好。
所以'的console.log(响应)'在成功处理程序提供了什么? – VisioN 2012-07-20 16:18:43
您网页上的哪些元素具有“问题”ID? – MrOBrian 2012-07-20 16:19:19
你为什么使用'datatype:“jsonp”'而不是'“json”'? – jessegavin 2012-07-20 16:19:34