我正在尝试删除我的应用程序中不必要的飞行前请求 . 为此,我简化了请求的一些部分,删除了自定义 Headers 等 . 但是遇到了问题 - GET请求现在可以在没有预先飞行的情况下正常工作,但POST请求仍然有它们 .
我遵循了要求:
-
请求未设置自定义HTTP标头 .
-
内容类型为"text/plain; charset=utf-8" .
-
请求方法必须是GET,HEAD或POST之一 . 如果是POST,则内容类型应为application / x-www-form-urlencoded,multipart / form-data或text / plain之一 .
GET和POST请求都通过单个httpinvoke调用 .
作为一个例子 - GET请求不是在飞行前开头的:
URL:http:// mydomain / APIEndpoint / GETRequest?Id = 346089&Token = f5h345请求方法:GET请求标头:接受:* / * Accept-Encoding:gzip,deflate Accept-Language:uk-UA,uk; q = 0.8 ,ru; q = 0.6,en-US; q = 0.4,en; q = 0.2 Cache-Control:no-cache连接:keep-alive内容类型:text / plain; charset = utf-8主机:正确的主机来源:http:// localhost Pragma:no-cache Referer:correct referer User-Agent:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,和Gecko一样) Chrome / 60.0.3112.90 Safari / 537.36查询字符串参数:Id = 346089令牌= f5h345
并且POST请求看起来非常相似,但仍以飞行前开头:
URL:http:// mydomain / APIEndpoint / GETRequest?param = TEST请求方法:POST请求标头:接受:* / * Accept-Encoding:gzip,deflate Accept-Language:uk-UA,uk; q = 0.8,ru ; q = 0.6,en-US; q = 0.4,en; q = 0.2 Cache-Control:no-cache连接:keep-alive内容长度:11内容类型:text / plain; charset = UTF-8主机:来源:http:// localhost Pragma:no-cache Referer:User-Agent:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 60.0 . 3112.90 Safari / 537.36查询字符串参数:param:TEST请求有效负载{MyData:{}}
任何意见,将不胜感激!谢谢!
====更新===
根据要求,发布POST请求的转发前请求:
URL:http:// mydomain / APIEndpoint / GETRequest?param = TEST请求方法:选项状态代码:200 OK响应标头访问控制允许原点:高速缓存控制:无高速缓存内容长度:0日期: 2017年8月9日星期三08:02:16 GMT到期:-1 Pragma:no-cache服务器:Microsoft-IIS / 8.5 X-AspNet-版本:4.0.30319 X-Powered-By:ASP.NET请求标头接受: / * Accept-Encoding:gzip,deflate Accept-Language:uk-UA,uk; q = 0.8,ru; q = 0.6,en-US; q = 0.4,en; q = 0.2 Access-Control-Request-Method: POST Cache-Control:no-cache连接:keep-alive主机:正确的主机来源:http:// localhost Pragma:no-cache Referer:correct referer User-Agent:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit /537.36(KHTML,与Gecko一样)Chrome / 60.0.3112.90 Safari / 537.36查询字符串参数参数:TEST
这是一个使用httpinvoke库的遗留代码 . 实际执行调用的代码段:
_converters: {
'text json': function (input, reviver) {},
'json text': JSON.stringify
};
headers = {
'Content-Type': 'text/plain; charset=utf-8'
};
data = {params: "test"};
httpinvoke(url, method.toUpperCase(), {
corsExposedHeaders: ['Content-Type'],
headers: headers,
input: data,
converters: _converters,
inputType: 'json',
outputType: 'json',
timeout: self._getMessageTimeout()
}).then(function (res) {}, function (error) {});
2 回答
如果在XMLHttpRequestUpload对象上注册了事件侦听器(强制执行预检;请参阅https://xhr.spec.whatwg.org/中的the note on the use-CORS-preflight flag,https://fetch.spec.whatwg.org/中的a related note和MDN CORS article中的the updated documentation on CORS preflight requests),则可能会发生这种情况 .
httpinvoke会那样做吗?
@Anne提到POST发送飞行前请求的原因尽管请求本身符合“简单请求”的规则(因此不需要预先飞行),但是在XMLHttpRequestUpload事件侦听器中 .
代码中可能没有提到XMLHttpRequestUpload本身,但您总是可以在xhr.upload变量中找到它 . 这是http-invoke库的情况 .
完全无辜的代码如下:
xhr.upload.onprogress = onuploadprogress;
实际上导致强制性的飞行前请求 .
感谢所有帮助解决这个问题的人 .