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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵