相关疑难解决方法(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万
查看次数

更改Content-Type时,Http-Method从POST更改为OPTIONS

我正在使用闭包库来做一个简单的POST.我认为XhrI应该工作,因为当我使用任何其他休息客户端时,我的机器,如Firefox浏览器应用程序RESTClient或Chrome的简单休息客户端,我可以向服务器发出POST请求,内容类型是application/json.

但是根据我的申请,我无法发帖.我使用以下代码

xhr = new goog.net.XhrIo;
xhr.send('http://myhost:8181/customer/add','POST', goog.json.serialize(data));
Run Code Online (Sandbox Code Playgroud)

如果我将标题保留为默认值,我会得到这个

Encoding: UTF-8
Http-Method: POST
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Run Code Online (Sandbox Code Playgroud)

如果我尝试通过传递{'content-type':'application/json'}第4个参数来更改标题,标题将更改为

Http-Method:选项
内容类型:

我是否应该能够使用Closure库正确地更改标头,就像RESTClient使用JQuery使用XMLHttpRequest一样?

如何更改标题以使其显示为这样

Encoding: UTF-8
Http-Method: POST
Content-Type: application/json;charset=UTF-8
Run Code Online (Sandbox Code Playgroud)

感谢Eddie的任何帮助

javascript xmlhttprequest google-closure plovr

4
推荐指数
1
解决办法
9828
查看次数