【发布时间】:2017-10-09 17:55:38
【问题描述】:
我有一个 ASP.NET MVC 5 项目,其中一个页面有一些 JavaScript 调用运行 ASP.NET WebApi 项目的服务器上的 API。这两个项目通常在不同的域上运行,但在本地它们只是在localhost 上的不同端口上。此 JavaScript 正在发出 GET 请求,但是当我查看网络日志时,正在发出 OPTIONS 请求,而 WebApi 返回 405 Method Not Authorized。
根据我的研究,我知道this is a "preflight" check that is sometimes triggered for AJAX calls,我知道ASP.NET WebApi doesn't support OPTIONS by default,所以这就是我得到405 的原因。但是,the preflight check is supposed to be skipped when certain conditions are met,并且这些条件在这里得到了满足。你自己看;以下是 AJAX 请求的(匿名的)完整的 JS 代码:
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:12345/api/SomeAction?someArg=" + someArg;
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
// [snip], process the result here
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
为什么这个简单的请求会触发预检?
【问题讨论】:
标签: asp.net ajax asp.net-mvc azure-application-insights