drjltlm156790
drjltlm156790
2016-07-08 19:25
浏览 71

AJAX响应返回有效的JSON,控制台日志未定义

So I have a peculiar issue with my AJAX request. When I make the request, I can click on the network tab and see the response, which is valid JSON. But when I try to use that data in my success function of the ajax call, it returns undefined. I'll post some pics of my console and network tab and some code.

Console

network

And at last my code:

getWebinars: function (from, to, credential) {
    $.ajax({
        url: 'endpoint',
        data: {
                     from: from,
                     to: to,
                     credential: credential
        },
        success: function (reply, status, xhr) {

            console.log(reply);
            var html = '<select class="webinars" multiple>';
            for(var webinar_id in reply.list) {
                html+=  '<option value="' + webinar_id + '">' + reply.list[webinar_id] + '</option>';
            }

            html+= '</select>';

             $('.modal-body').append(html);
        }
    });
}

I can't figure out for the life of me what the issue is here. I have tried everything I can think of. Logging the xhr, logging the responseJSON, setting the datatype to json. It's a simple get request and it's working, but the console doesn't seem to think so. Any help is appreciated.

图片转代码服务由CSDN问答提供 功能建议

所以我的AJAX请求有一个特殊的问题。 当我发出请求时,我可以单击网络选项卡并查看响应,这是有效的JSON。 但是当我尝试在我的ajax调用的成功函数中使用该数据时,它返回undefined。 我将发布我的控制台和网络选项卡以及一些代码的一些图片。

最后我的代码:

  getWebinars:function(from,to,credential){
  $ .ajax({
 url:'endpoint',
 data:{
 from:from,
 to:to,
 credential:credential 
},
 success:function(reply,status,xhr)  ){
 
 console.log(回复); 
 var html ='&lt; select class =“webinars”multiple&gt;'; 
 for(var webinar_id in reply.list){
 html + ='&lt;  option value =“'+ webinar_id +'”&gt;'+ reply.list [webinar_id] +'&lt; / option&gt;'; 
} 
 
 html + ='&lt; / selec  t&gt;'; 
 
 $('。modal-body')。append(html); 
} 
}); 
} 
   
 
 

我无法弄清楚我的生活是什么问题。 我已经尝试了我能想到的一切。 记录xhr,记录responseJSON,将数据类型设置为json。 这是一个简单的获取请求,它正在工作,但控制台似乎并不这么认为。 任何帮助表示赞赏。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzha5934
    dongzha5934 2016-07-08 21:16
    已采纳

    ok, so I figured this one out, sort of. I should say "I fixed it" I still don't know why it did what it did. For whatever reason, jQuery was executing the success callback well before the request was complete, hence my undefined issue. I switched it over to good ol' vanilla javascript and it worked fine. As to why it happened with jquery, I don't have an answer for that. Hopefully this will help others out.

    点赞 评论
  • dtp0760
    dtp0760 2016-07-08 20:12

    Your root cause is you don't mention dataType:'JSON' as parameter of your AJAX request. So there is two ways to resolve your problem. 1.Add dataType parameter. 2.Parse the returned result using jQuery.parseJSON().

    Solution 1

    url: 'endpoint',
    data: {
           from: from,
           to: to,
           credential: credential
            },
    dataType:'text json'

    solution 2

    var obj = jQuery.parseJSON(reply);
    alert( obj.list);

    </div>
    
    点赞 评论

相关推荐