[英]Setting a custom content-type xmlhttprequest
I have an API I am trying to interface with that requires a custom content-type header be set, with the value text/xmlmc
我正在尝试与之交互的API,需要设置一个自定义内容类型标头,其值为
text/xmlmc
I've implemented this like so 我已经这样实现了
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);
};
The endpoint is localhost:5015
端点是
localhost:5015
When I do this, the request fails and never even sends. 当我这样做时,请求失败,甚至从不发送。 When I use a standard request header like
'text/plain'
the request is sent but returns a status code of 501 not implemented. 当我使用
'text/plain'
之类的标准请求标头时,将发送请求,但返回未实现的状态代码501。 How can I set a custom HTTP header in an xmlhttprequest? 如何在xmlhttprequest中设置自定义HTTP标头?
It turns out this was due to a cross origin issue. 事实证明,这是由于跨源问题。 Even when the domain is the same, if the ports are different it is a problem.
即使域相同,如果端口不同,也是一个问题。 I fixed the issue by adding a reverse proxy to my apache configuration and now I can query the api without cross origin requests.
我通过将反向代理添加到我的apache配置中解决了该问题,现在我可以查询api,而无需交叉来源请求。 Unfortunately I don't have access to change the API and allow cross origin domains.
不幸的是,我无权更改API并允许跨源域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.