Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 通过.ajax的JSON请求-如何利用_Jquery_Json_Ajax - Fatal编程技术网

Jquery 通过.ajax的JSON请求-如何利用

Jquery 通过.ajax的JSON请求-如何利用,jquery,json,ajax,Jquery,Json,Ajax,我迷路了:-(我通过CORS收到来自AJAX请求的成功响应,但我不知道如何实际使用我正在获取的JSON文件 我的所有警报都正常工作。如何将收到的JSON文件缓存为函数/变量 $(function(){ var pulseRestApiGetQueryUri = "https://doesntmatter/api/v1/search.json?q=" + encodeURIComponent("%23c2alerts"); $.support.cors = true;

我迷路了:-(我通过CORS收到来自AJAX请求的成功响应,但我不知道如何实际使用我正在获取的JSON文件

我的所有警报都正常工作。如何将收到的JSON文件缓存为函数/变量

$(function(){
    var pulseRestApiGetQueryUri = "https://doesntmatter/api/v1/search.json?q=" + encodeURIComponent("%23c2alerts");
    $.support.cors = true;                                                                                    
    $.ajax({
         type: "GET",
         headers: {'X-API-Key': 'com.amazon.c2alerts'},                                               
         url: pulseRestApiGetQueryUri,                                                                         
         timeout: 10000, // 10 seconds
         contentType: "application/json; charset=utf-8",                                                       
         dataType: "json",
         xhrFields: {withCredentials: true},

         success: function(posts) {
            alert('SUCCESS');
         },
         error: function(jqXhr, status, errorThrown) {
            alert('ERROR');
         },
         beforeSend: function() {
            alert('BUSY');
         },
         complete: function() {
            alert('COMPLETE');
         }
    });
});
除了“成功”之外,每个警报都有效。在“完成”之后,我可以做任何我想做的事情。如何使结构随时可用于HTML操作,以及将其放置在何处

下面是我的JSON中的两个示例条目

{
     "author":{
        "namespace":"user",
        "name":"nichazel",
        "string_form":"user:nichazel",
        "full_name":"Nicholas Hazel"
     },
     "body":"Have an idea? We are trying to collect new and fresh ideas for process improvement. Please jot down ANY ideas you may have. We will discuss them in our team meeting this afternoon -urgent #c2alerts",
     "topics":[
        {
           "namespace":"hashtag",
           "name":"c2alerts",
           "string_form":"hashtag:c2alerts"
        }
     ],
     "source":"web",
     "post_id":"30d97e00-596f-4936-ade9-557db0e907df",
     "created":"2013-07-31T20:18:22Z",
     "votes":{
        "up_votes":2,
        "down_votes":0,
        "up_voters":[
           {
              "namespace":"user",
              "name":"bostrom",
              "string_form":"user:bostrom"
           },
           {
              "namespace":"user",
              "name":"eakerry",
              "string_form":"user:eakerry"
           }
        ],
        "down_voters":[

        ]
     }
  },
  {
     "author":{
        "namespace":"user",
        "name":"chayavic",
        "string_form":"user:chayavic",
        "full_name":"Sam Chayavichitsilp"
     },
     "body":"Happy Friday C2. Retail AHOD (L2) - No Stand-Up Meeting for the entire team.\n#c2alerts",
     "topics":[
        {
           "namespace":"hashtag",
           "name":"c2alerts",
           "string_form":"hashtag:c2alerts"
        }
     ],
     "source":"web",
     "post_id":"a05d96ae-2c6e-4054-989f-d25a74bfc553",
     "created":"2013-07-26T14:57:18Z"
  },
下面是一个通过getJSON工作的示例(我不能使用它,因为它不是CORS):

相同的原则不适用于常规的AJAX请求。您知道如何将JSON作为DOM的一部分,或者至少对其进行解析以利用字段吗?

使用
JSON.parse()
将JSON格式的字符串转换为对象:

success: function(posts) {
    someVariable = JSON.parse(posts);
}
如果您的成功回调没有触发,那么您还有另一个问题。错误的输出是什么

error: function(jqXhr, status, errorThrown) {
    console.log(jqXhr);
    console.log(status);
    console.log(errorThrown);
}

由于您要求它发出警报,因此它正在发送成功消息。对于迭代JSON,JSON可以存储在一个变量中,并且由于您已经有
posts
,您可以使用它在SUCCESS函数中使用循环进行迭代,如下所示:

for(var i=0;i<posts.length;i++){
    alert(posts[i].author);
    alert(posts[i].body);
    //Etc. Other fields can be well added.
}

用于(var i=0;我从
$.getJSON
中移动
$。每个
,并将其放置在成功回调中……当我查看FireBug中的文件时,我会查看该文件,JSON文件的“响应”会显示我的所有数据。是否存在这样的情况:它会提取所有数据,但不会触发“成功”?@NicholasHazel是的,在这种情况下错误将被触发。令人惊讶的是,非常平淡…我收到了一个错误500-内部服务错误。除此之外,“ErrorSprown”是未定义的:对象{readyState=0,status=0,statusText=“ERROR”}错误(一个空字符串)已完成test2.html(第34行)好吧,这至少解释了为什么你的成功函数没有启动。如果没有更具体的细节,我无法进一步帮助你。你可以尝试弄乱标题或删除参数(例如
dataType
参数)。500 error表示与jquery无关的服务器端错误。使用服务器端调试获取更多细节。
for(var i=0;i<posts.length;i++){
    alert(posts[i].author);
    alert(posts[i].body);
    //Etc. Other fields can be well added.
}