IE 11 Ajax调用引发错误0x2efe或未提供身份验证凭据

时间:2018-11-08 17:58:14

标签: javascript django django-rest-framework internet-explorer-11

我有一个非常简单的ajax PUT调用,该调用可在Chrome和Firefox中使用,但在IE 11中不可用。

服务器正在为IE11发送403_FORBIDDEN状态,为chrome和firefox发送200_OK状态。

我在IE11开发工具(f12)中遇到两个错误之一:

SCRIPT7002: XMLHttpRequest: Network Error 0x2efe, Could not complete the operation due to error 00002efe.

基础Error: responseText"{"detail":"Authentication credentials were not provided."}"

这是我的html:

{% csrf_token %}
<select name="1_score_risk" data-score="risk"
                            data-assesspk="1" data-answerpk="1"
                            class="select-post form-control center-block">
        <option selected value=0>0</option>
        <option value=1>1</option>
        <option value=2>2</option>
</select>

和我的csrf_token设置标头javascript:

//this is directly from https://docs.djangoproject.com/en/dev/ref/csrf/
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

var csrftoken = getCookie('csrftoken');

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

$.ajaxSetup({
    beforeSend: function (xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            console.log(csrftoken); //csrf token shows up successfully every time
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});

基于选择输入的“ on change”的实际AJAX调用。

$(".select-post").on("change", function (e) {
    console.log($(this).val());
    var answerpk = $(this).data("answerpk");
    var score_type = $(this).data("score");
    var url = "{% url 'wsva:answer-list' %}" + answerpk + "/";
    var score_str = "score_" + score_type;
    var score = $(this).val();
    var payload = {};
    payload[score_str] = score;
    console.log('payload: ');
    console.log(JSON.stringify(payload));
    $.ajax({
        url: url,
        type: 'PUT',
        data: JSON.stringify(payload),
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        cache: false,
        xhrFields: {withCredentials: true},
        success: function (data) {
            console.log('put complete');
        },
        error: function (jqXHR, exception) {
            // Note: Often ie will give no error msg. Aren't they helpful?
            console.log('ERROR: jqXHR, exception', jqXHR, exception );
        }
    });
});

我已使用CORS_ORIGIN_ALLOW_ALL = True正确安装了django-cors-header

我试图先在本地主机上进行调试,但即使在测试环境中也出现错误。

0 个答案:

没有答案