开发者

jquery JSON making an OPTIONS request despite GET being set

I am having an odd problem on Windows whilst using any browser. When I make a request from my local machine to an external website with a JSON file. Apache receives an OPTIONS request rather than a GET despite GET being specified. After some research it looks like a cross site request issue however, most of the articles I found where old/ the bugs have been fixed. Has anyone any idea as to why this is happening and how it can be resolved.

Thanks

$.ajax({
 type: 'GET',
 url: http://mywebsite.com/getjsn.json,
 dataType: "json",
 cache: false,
 success: function(data, textStatus, XMLHttpRequest) {
  // do something.
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
  // deal with error.
 },
 complete: function(XMLHttpRequest, textStatus) {
  // all do开发者_StackOverflow社区ne.
 }
});

asdasd


I suggest you look at http://api.jquery.com/jQuery.ajax/, specifically in regards to JSONP transactions. When you're loading JSON from an external website, you have to structure your request differently. You will likely need to adjust your server to accommodate for this as well. The end call will look something more like this.

$.ajax({
    url: 'http://mywebsite.com/getjson.json?callback=?',
    dataType: 'jsonp',
    success: function(data) {
        // do something.
    }
});

As for the 'OPTIONS' request itself, I believe this is explained in developer.mozilla.org/En/HTTP_access_control . I'd imagine James is using a modern browser, and that browser is trying to ask the target server whether or not the request should be allowed. This isn't supported in all browsers though, so I still suggest my previous solution.


The options request is done by the browser, as a safeguard for cross site scripting attacks. Javascript has no idea it ever happens, it is the browser doing it. You need to have your server return the proper access control headers if your request is from a different domain. If the get request is from the same domain, you won't ever see the options request.

For sites with with everything routed through index.php, something similar to this at the beginning will work:

if (PHP_SAPI != 'cli'){
    $allowed = array('http://domainyouwantthebrowertobeabletocomefrom.com', 'http://blabla.com');
    if (isset($_SERVER['HTTP_ORIGIN'])){
        if (in_array($_SERVER['HTTP_ORIGIN'], $allowed) OR substr($_SERVER['HTTP_ORIGIN'], -11) == 'netmark.dev'){
            header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
            header( "Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS" );
            header( 'Access-Control-Allow-Headers: accept, stamp, signature, x-requested-with, origin, content-type' );
            header( 'Access-Control-Allow-Credentials: true' );
            header( 'Access-Control-Max-Age: 300' );
        }
    }
    if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
        exit;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜