[英]Setting a custom content-type xmlhttprequest
我正在嘗試與之交互的API,需要設置一個自定義內容類型標頭,其值為text/xmlmc
我已經這樣實現了
Xmlmc.prototype.submitRequest = function (request,callback) {
var self = this;
var port = portMap[request.service] || 5015;
var endpoint = this.endpoint = 'http://' + this.server + ':' + port;
var xml = request.toXml();
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// IE 5 and 6 makes us sad. Please don't use it
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//handle request
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4) {
var response = self.handleResponse(xhttp);
callback(response);
}
};
xhttp.open('POST',endpoint,true);
xhttp.setRequestHeader('Content-type', 'text/xmlmc');
//xhttp.setRequestHeader('Content-length', xml.length.toString());
if(this.sessionCookie != '') {
xhttp.setRequestHeader('Cookie', this.sessionCookie);
}
xhttp.send(xml);
};
端點是localhost:5015
當我這樣做時,請求失敗,甚至從不發送。 當我使用'text/plain'
之類的標准請求標頭時,將發送請求,但返回未實現的狀態代碼501。 如何在xmlhttprequest中設置自定義HTTP標頭?
事實證明,這是由於跨源問題。 即使域相同,如果端口不同,也是一個問題。 我通過將反向代理添加到我的apache配置中解決了該問題,現在我可以查詢api,而無需交叉來源請求。 不幸的是,我無權更改API並允許跨源域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.