ASP MVC 3 - Jquery Ajax 总是发送选项而不是 Post
我有一个简单的 javascript ajax 请求:
$("#login-submit").button().click(function() {
var ajaxOptions = {
url: "http://localhost:29097/authentication/",
type: "POST",
data: $("login-form").serialize(),
success: function(result) { $("body").html(result); }
};
$.ajax(ajaxOptions);
});
但是,每当我发送请求时,它总是发送 OPTIONS 请求,而不是指定的 POST 请求。经过更多阅读后,似乎可能归因于跨网站发布。我在我的 MVC 应用程序中添加了一个自定义操作过滤器,它检查 OPTION 请求并使用指定“GET、POST、PUT、DELETE”的“Allow”标头进行响应,尽管它似乎从未执行后续 POST 请求...我是吗?这里缺少一些东西吗?
请求(在 Firefox、Chrome 中尝试过):
OPTIONS http://localhost:29097/authentication/ HTTP/1.1
Host: localhost:29097
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: null
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
响应:
HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/10.0.0.0
Date: Sun, 19 Jun 2011 16:20:41 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Allow: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *
Access-Control-Allow-Origin: *
Cache-Control: private
Content-Length: 0
Connection: Close
我自己通过 MVC 发回 401 请求,因为它似乎是用于指示用户凭据无效的最佳标头。此状态代码会导致任何问题吗?
I have a simple javascript ajax request:
$("#login-submit").button().click(function() {
var ajaxOptions = {
url: "http://localhost:29097/authentication/",
type: "POST",
data: $("login-form").serialize(),
success: function(result) { $("body").html(result); }
};
$.ajax(ajaxOptions);
});
However whenever I send off the request, it always sends an OPTIONS request, not the specified POST request. After reading into it more it seems like it may be down to cross site posting. I have added a custom action filter in my MVC app which checks for OPTION requests and responds with an "Allow" header specifying "GET, POST, PUT, DELETE" although it never seems to do a follow up POST request... Am I missing something here?
The request (tried this in Firefox, Chrome):
OPTIONS http://localhost:29097/authentication/ HTTP/1.1
Host: localhost:29097
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: null
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
The response:
HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/10.0.0.0
Date: Sun, 19 Jun 2011 16:20:41 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Allow: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *
Access-Control-Allow-Origin: *
Cache-Control: private
Content-Length: 0
Connection: Close
I am sending back the 401 request myself through MVC, as it seemed like it would be the best header to use to indicate that the users credentials were invalid. Could this status code be causing any problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的浏览器实现了跨源资源共享。为了支持这一点,您的服务器需要使用 Access-Control-Allow-Origin 标头响应 OPTIONS 请求。
You are using a browser that implements Cross Origin Resource Sharing. To support this your server needs to respond to the OPTIONS request with a Access-Control-Allow-Origin header.