开发者

jQuery: I get OPTIONS request instead of GET

开发者 https://www.devze.com 2022-12-11 19:39 出处:网络
I am using simple jQuery $.get( .... ); Here instead of getting GET response I get OPTIONS.( checked in firebug Net)

I am using simple jQuery

$.get( .... );

Here instead of getting GET response I get OPTIONS.( checked in firebug Net)

Same code is working fine in Safari. Looks like some problem with Firefox.

Any wo开发者_开发问答rkaround / solutions to fix this problem..

Thanks

Kurund


The OPTIONS request what you see is the preflight request, you can read about that here:

  • 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.

There are two solutions to solve the problem (as mentioned above):

  • implement the response for the OPTIONS request with the corresponding Access-Control-* headers
  • use a JSONP request instead of simple JSON


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.

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) :)

Hope that helps!


I had the same issue, the cause I figured was in the html <head> section I had set the base element to this

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

Which I changed to

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


I hope this helps someone: http://kurund.com/blog/2010/09/09/how-to-call-external-site-url-using-jquery-ajax/


You are sending request to cross domain.

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.

So you may need change specify contentType to avoid OPTION request. Example:-

$.ajax({
    url: "crossdomainurl",
    type: "GET",
    contentType: 'text/plain'
}); 
0

精彩评论

暂无评论...
验证码 换一张
取 消