jQuery:我收到 OPTIONS 请求而不是 GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1743845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: I get OPTIONS request instead of GET
提问by Kurund Jalmi
I am using simple jQuery
我正在使用简单的 jQuery
$.get( .... );
Here instead of getting GET response I get OPTIONS.( checked in firebug Net)
在这里,我得到的不是 GET 响应,而是 OPTIONS。(在 firebug Net 中检查)
Same code is working fine in Safari. Looks like some problem with Firefox.
相同的代码在 Safari 中运行良好。Firefox 看起来有些问题。
Any workaround / solutions to fix this problem..
解决此问题的任何解决方法/解决方案..
Thanks
谢谢
Kurund
库伦德
采纳答案by Kurund Jalmi
回答by KARASZI István
The OPTIONS
request what you see is the preflight request, you can read about that here:
OPTIONS
你看到的请求是预检请求,你可以在这里阅读:
- https://developer.mozilla.org/En/HTTP_access_control
- http://www.w3.org/TR/cors/
- http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx
- https://developer.mozilla.org/En/HTTP_access_control
- http://www.w3.org/TR/cors/
- http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx
It's there because you're requesting a cross-domain XMLHttpRequest so the browser has to check whether your request is allowed on the remote server or not.
它在那里是因为您正在请求跨域 XMLHttpRequest,因此浏览器必须检查远程服务器上是否允许您的请求。
There are two solutions to solve the problem (as mentioned above):
有两种解决方案可以解决问题(如上所述):
- implement the response for the
OPTIONS
request with the correspondingAccess-Control-*
headers - use a JSONPrequest instead of simple JSON
OPTIONS
使用相应的Access-Control-*
标头实现对请求的响应- 使用JSONP请求而不是简单的 JSON
回答by fitzgeraldsteele
This is likely due to restrictions on Javascript doing cross-domain XMLHttpRequests. This is generally not allowed for security reasons. See the question referenced above, or a similar question I asked.
这可能是由于 Javascript 执行跨域 XMLHttpRequests 的限制。出于安全原因,这通常是不允许的。请参阅上面引用的问题,或我问的类似问题。
To solve this problem:
要解决这个问题:
- Write a sever side component (using PHP or whatever) that will retrieve the remote resource on behalf of your AJAX request, or
- Do a JSONP call: see http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html(or hunt around StackOverflow for JSONP) :)
- 编写一个服务器端组件(使用 PHP 或其他)代表您的 AJAX 请求检索远程资源,或
- 进行 JSONP 调用:请参阅http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html(或在 StackOverflow 中寻找 JSONP):)
Hope that helps!
希望有帮助!
回答by Moak
I had the same issue, the cause I figured was in the html <head>
section I had set the base element to this
我遇到了同样的问题,我认为原因是在 html<head>
部分中,我已将基本元素设置为此
<base href="http://local.develepment.url" />
Which I changed to
我改成
<base href="http://<?php echo $_SERVER['HTTP_HOST']?>/" />
回答by Bala
You are sending request to cross domain.
您正在向跨域发送请求。
For cross-domainrequests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plainwill trigger the browser to send a preflight OPTIONSrequest to the server.
对于跨域请求,将内容类型设置为application/x-www-form-urlencoded、multipart/form-data或 text/plain 以外的任何内容将触发浏览器向 服务器发送预检 OPTIONS请求。
So you may need change specify contentTypeto avoid OPTION request. Example:-
因此,您可能需要更改指定contentType以避免 OPTION 请求。例子:-
$.ajax({
url: "crossdomainurl",
type: "GET",
contentType: 'text/plain'
});