0

我有一个要求,我需要获取在特定 url(在 JSON 中)中生成的内容。在我的 jQuery ajax 调用之后,我需要将此输出分配给我的 json 对象。我已经使用以下代码进行了尝试,但没有产生任何结果(在成功函数参数中始终为 null)。

我的 Jquery ajax 调用代码如下,

     $.ajax({
       type: "GET",
       url: "http://some url",
       data : {},
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (obj) {               
           alert(obj);
           var test = eval("(" + obj + ")");             
           alert(test);        

       },
       error: function () {
           alert("error");
       }
   })

我的请求有什么错误吗?如果这不是正确的方法,请为我​​的情况提出一种方法。

4

1 回答 1

0

你这里有几个问题。首先,您将无法提醒它。利用

console.log(obj);

然后在 Firebug 中查看。

至于您的请求,您需要遍历它们(如果数据为空,您可以完全排除数据)......

     $.ajax({
       type: "GET",
       url: "http://some url",
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (data) {   
               $.each(data, function(idx, obj){    
                    console.log(obj);
               });              
       },
       error: function () {
           alert("error");
       }
   })

如果 URL 实际上为您提供了有效的 json,那应该可以工作。

于 2011-06-20T05:32:42.310 回答