2012-07-20 21 views
0

我找回一些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" 
     } 
    ] 
} 
+0

所以'的console.log(响应)'在成功处理程序提供了什么? – VisioN 2012-07-20 16:18:43

+0

您网页上的哪些元素具有“问题”ID? – MrOBrian 2012-07-20 16:19:19

+0

你为什么使用'datatype:“jsonp”'而不是'“json”'? – jessegavin 2012-07-20 16:19:34

回答

0
$('#questions').val(res.q); 

根据您的你想设置什么,你可能需要使用:

$('#questions').html(res.q); 
+0

也许不应该使用一个id,除非一次只显示一个问题。 – MrOBrian 2012-07-20 16:27:13

+0

这是目前只是测试,以确保thigns回来:) – thatuxguy 2012-07-20 16:29:04

+0

我目前有一个文本框与问题的ID只是为了确保我可以得到回报:) – thatuxguy 2012-07-20 16:35:31

0

随着dataTypejsonp响应预期是一个JSON斑点包裹在一个函数调用。由于您只返回JSON,因此您应该使用jsondataType

我调整了您的示例以使用JSFiddle echo API。在first version中,我使用了代码中使用的jsonpdataType。正如你看到,如果你访问该链接,它不起作用。在edited version我用dataTypejson它工作得很好。