javascript - jQuery ajax - xhr.responseText 始终未定义

标签 javascript jquery ajax

我正在尝试调试对 API 的 jQuery 调用,但是 xhr.responseText 似乎总是未定义。我需要对此代码进行哪些更改才能查看错误的详细信息?

jQuery.ajax({
    url: 'http://fusion-api-user.dev/api/v1/user/authorise/' + currentVideoItem.id + '/play',
    async: async,
    beforeSend: function(xhr){
        xhr.setRequestHeader('X-Forwarded-Host', 'fusion.dev');
    },
    xhrFields: {
        withCredentials: true
    },
    success: function (res, status, xhr) {
        currentVideoItem.authModule.playWithToken(xhr.getResponseHeader("X-Token"), "ais");
        alert('xhr: ' + xhr);
        alert('token:' + xhr.getResponseHeader('X-Token'));

        jQuery(currentVideoItem.parentContainer).trigger('video-authorized', [{
            id: currentVideoItem.id
        }]);
    },
    error: function(xhr, status) {
        alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
        alert("responseText: " + xhr.responseText);
        jQuery(window).trigger('countdown-session-ended', [{
            name: 'video', 
            trigger: 'authorizePlayError'
        }]);
    }
});

最佳答案

我通过添加回调函数并在成功函数中调用它来解决这个问题,如下所示:

success: function (response) {
//
    callback(response);
//
},

关于javascript - jQuery ajax - xhr.responseText 始终未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32348220/

相关文章:

javascript - 如果 window.location.href 里面有 'sometext' - 这样做

javascript - 比较日期时间逻辑

javascript - 如果用户使用移动设备,则禁用 download_manager

javascript - 帮助在 JavaScript 中进行日期转换

javascript - 向下滚动时添加到居中导航栏图标/ Logo /图像的过渡

javascript - XHR 进度事件在上传完成之前不会触发?

jquery - 我应该使用哪个库?

jquery - 如何让标题上滑时图片也上滑?

php - 设置 WooCommerce 结算公司后禁用特定付款方式

javascript - 如何获取DELETE请求参数