jQuery $.ajax(), $.post sending “OPTIONS” as REQUEST_METHOD in Firefox

前端 未结 23 2553
傲寒
傲寒 2020-11-22 14:38

Having trouble with what I thought was a relatively simple jQuery plugin...

The plugin should fetch data from a php script via ajax to add options to a

相关标签:
23条回答
  • 2020-11-22 15:20

    Try to add the following:

    dataType: "json",
    ContentType: "application/json",
    data: JSON.stringify({"method":"getStates", "program":"EXPLORE"}),  
    
    0 讨论(0)
  • 2020-11-22 15:21

    This PHP at the top of the responding script seems to work. (With Firefox 3.6.11. I have not yet done a lot of testing.)

    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    if(array_key_exists('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', $_SERVER)) {
        header('Access-Control-Allow-Headers: '
               . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
    } else {
        header('Access-Control-Allow-Headers: *');
    }
    
    if("OPTIONS" == $_SERVER['REQUEST_METHOD']) {
        exit(0);
    }
    
    0 讨论(0)
  • 2020-11-22 15:21

    I already have this code handling well my cors situation in php:

    header( 'Access-Control-Allow-Origin: '.CMSConfig::ALLOW_DOMAIN );
    header( 'Access-Control-Allow-Headers: '.CMSConfig::ALLOW_DOMAIN );
    header( 'Access-Control-Allow-Credentials: true' );
    

    And it was working fine locally and remotely, but not for uploads when remote.

    Something happen with apache/php OR my code, I didn't bother to search it, when you request OPTIONS it returns my header with cors rules but with 302 result. Therefore my browser doesn't recognise as an acceptable situation.

    What I did, based on @Mark McDonald answer, is just put this code after my header:

    if( $_SERVER['REQUEST_METHOD'] === 'OPTIONS' )
    {
        header("HTTP/1.1 202 Accepted");
        exit;
    }
    

    Now, when requesting OPTIONS it will just send the header and 202 result.

    0 讨论(0)
  • 2020-11-22 15:22

    The reason for the error is the same origin policy. It only allows you to do XMLHTTPRequests to your own domain. See if you can use a JSONP callback instead:

    $.getJSON( 'http://<url>/api.php?callback=?', function ( data ) { alert ( data ); } );
    
    0 讨论(0)
  • 2020-11-22 15:22

    Solution to this is:

    1. use dataType: json
    2. add &callback=? to your url

    this worked on calling Facebook API and with Firefox. Firebug is using GET instead of OPTIONS with the above conditions (both of them).

    0 讨论(0)
  • 2020-11-22 15:23

    I've fixed this issue using an entirely-Apache based solution. In my vhost / htaccess I put the following block:

    # enable cross domain access control
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"
    
    # force apache to return 200 without executing my scripts
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule .* / [R=200,L]
    

    You may not need the latter part, depending on what happens when Apache executes your target script. Credit goes to the friendly ServerFault folk for the latter part.

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