我正在开发一个内部Web应用程序.在IE10中,请求工作正常,但在Chrome中,所有AJAX请求(有很多)都是使用OPTIONS发送的,而不是我给出的任何定义的方法.从技术上讲,我的请求是"跨域".该站点在localhost:6120上提供,我正在向AJAX请求的服务是在57124. 这个关闭的jquery错误定义了问题,但不是真正的修复.
如何在ajax请求中使用正确的http方法?
编辑:
这是在每个页面的文档加载中:
jQuery.support.cors = true;
Run Code Online (Sandbox Code Playgroud)
每个AJAX都是类似的:
var url = 'http://localhost:57124/My/Rest/Call';
$.ajax({
url: url,
dataType: "json",
data: json,
async: true,
cache: false,
timeout: 30000,
headers: { "x-li-format": "json", "X-UserName": userName },
success: function (data) {
// my success stuff
},
error: function (request, status, error) {
// my error stuff
},
type: "POST"
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试发出POST请求,但我无法让它工作:
testRequest() {
var body = 'username=myusername?password=mypassword';
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http
.post('/api',
body, {
headers: headers
})
.subscribe(data => {
alert('ok');
}, error => {
console.log(JSON.stringify(error.json()));
});
}
Run Code Online (Sandbox Code Playgroud)
我基本上想要复制这个http请求(不是ajax),就像它是由html表单生成的:
网址:/ api
参数:用户名和密码