相关疑难解决方法(0)

jquery.ajax()的跨源"授权"标题

我正在尝试发送跨域域并添加自定义"授权"标头.请参阅下面的代码.

错误:

XMLHttpRequest无法加载{url}.请求标头字段Access-Control-Allow-Headers不允许授权.

function loadJson(from, to) {
    $.ajax({
        //this is a 'cross-origin' domain
        url : "http://localhost:2180/api/index.php",
        dataType : 'json',
        data : { handler : "statistic", from : from, to : to
        },
        beforeSend : setHeader,
        success : function(data) {
            alert("success");
        },
        error : function(jqXHR, textStatus, errorThrown) {
            alert("error");
        }
    });
}

function getToken() {
    var cookie = Cookie.getCookie(cookieName);
    var auth = jQuery.parseJSON(cookie);
    var token = "Token " + auth.id + ":" + auth.key;
}

function setHeader(xhr) {
    xhr.setRequestHeader('Authorization', getToken());
} …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery cross-domain

40
推荐指数
1
解决办法
9万
查看次数

标签 统计

ajax ×1

cross-domain ×1

javascript ×1

jquery ×1