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条)

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?