views:

33

answers:

1

Hello,

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 done.
 }
});

asdasd

+1  A: 

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.

BBonifield
This doesn't explain why a `HTTP GET` request would come in as a `HTTP OPTIONS` request. jQuery doesn't set anything of the sort.
Crescent Fresh
Have you inspected the headers "sent" to the server? if not please do so and append to your OP
RobertPitt
I believe this is explained in https://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.
BBonifield
@BBonifield: nice! That should go into your answer, it is very informative.
Crescent Fresh