drjltlm156790 2016-07-08 19:25
浏览 91
已采纳

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.

  • 写回答

2条回答 默认 最新

  • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?