jQuery:我得到OPTIONS请求而不是GET

jQuery: I get OPTIONS request instead of GET

我使用的是简单的jQuery

1
$.get( .... );

这里没有获得GET响应,而是获得OPTIONS。(在firebug Net中检查)

相同的代码在Safari中正常工作。 看起来像Firefox的一些问题。

解决此问题的任何解决方法/解决方案..

谢谢

Kurund


你看到的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

它就在那里,因为您正在请求跨域XMLHttpRequest,因此浏览器必须检查您的请求是否在远程服务器上被允许。

解决问题有两种解决方案(如上所述):

  • 使用相应的Access-Control-*标头实现OPTIONS请求的响应
  • 使用JSONP请求而不是简单的JSON


我有同样的问题,我想到的原因是在html 部分我已经设置了基本元素

1
<base href="http://local.develepment.url" />

我改成了

1
<base href="http://<?php echo $_SERVER['HTTP_HOST']?>/" />


这可能是由于Javascript执行跨域XMLHttpRequests的限制。出于安全原因,通常不允许这样做。请参阅上面提到的问题,或者我提出的类似问题。

要解决这个问题:

  • 编写将代表您的AJAX请求检索远程资源的服务器端组件(使用PHP或其他),或
  • 做一个JSONP调用:请参阅http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html(或者围绕StackOverflow搜索JSONP):)

希望有所帮助!


您正在向跨域发送请求。

For cross-domain requests, setting the content type to anything other
than application/x-www-form-urlencoded, multipart/form-data, or
text/plain will trigger the browser to send a preflight OPTIONS
request to the server.

因此,您可能需要更改指定contentType以避免OPTION请求。
例:-

1
2
3
4
5
$.ajax({
    url:"crossdomainurl",
    type:"GET",
    contentType: 'text/plain'
});

我希望这可以帮助别人:

How to call external site url using jQuery / AJAX