chrome开发人员工具的“响应”选项卡中有可用数据时,在AJAX响应中获取“未定义”

时间:2019-09-24 12:31:35

标签: javascript jquery ajax get cors

我正试图从以下跨域ajax请求中获取响应,但未成功定义:

$.ajax({
            type: "GET",
            url: "https://path_to_app/rest/jjwt",
            crossDomain: true,
            dataType: 'script',
            contentType: 'text/plain;charset=UTF-8',
            beforeSend: function(xhr) {
                xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
                xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
            },
            success: function (response, textStatus, xhr) {
                console.log(response);
                console.log(textStatus);
            },
            error: function (jqXHR, exception) {
                var msg = '';
                if (jqXHR.status === 0) {
                    msg = 'Not connect.\n Verify Network.';
                } else if (jqXHR.status == 404) {
                    msg = 'Requested page not found. [404]';
                } else if (jqXHR.status == 500) {
                    msg = 'Internal Server Error [500].';
                } else if (exception === 'parsererror') {
                    msg = 'Requested JSON parse failed.';
                } else if (exception === 'timeout') {
                    msg = 'Time out error.';
                } else if (exception === 'abort') {
                    msg = 'Ajax request aborted.';
                } else {
                    msg = 'Uncaught Error.\n' + jqXHR.responseText;
                }
                console.log(msg);
            }
        });

请参阅以下开发工具中的响应: enter image description here

当尝试在浏览器中使用完全相同的url时,我可以看到授权后的预期响应(令牌): server response

0 个答案:

没有答案