jQuery: I get OPTIONS request instead of GET

前端 未结 5 604
滥情空心
滥情空心 2020-12-05 15:25

I am using simple jQuery

$.get( .... );

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

Same code is w

相关标签:
5条回答
  • 2020-12-05 15:31

    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']?>/" />
    
    0 讨论(0)
  • 2020-12-05 15:33

    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 讨论(0)
  • 2020-12-05 15:37

    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!

    0 讨论(0)
  • 2020-12-05 15:48

    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
    0 讨论(0)
  • 2020-12-05 15:52

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

    0 讨论(0)
提交回复
热议问题