1

I'm having trouble getting data from a JSON feed in IE.

Here's the test. It alerts in FF, Chrome, Safari, but not IE:

function do_something(data){
    alert(data);
}

$(document).ready(function() {
    $.getJSON('https://www.mec.ca/api/v1/stores/1', do_something);
});

​ And here's the fiddle: http://jsfiddle.net/upksp/

I have tried the following solutions to seemingly-related questions with no luck:

I can't help but think it may be a problem with the headers, but don't know enough about them to diagnose. I've successfully used JSONP on the same page with no issues, and I don't see any conflicts. Help!


Note: For the code I'm working on, the API will be on the same domain(1) as the script that is requesting it. However for testing, they will have different subdomains(2) such as www.domain.com vs environment.domain.com. I assume (2) will be problematic, but (1) will work fine?

Community
  • 1
  • 1
chicgeek
  • 486
  • 3
  • 16
  • You cannot perform a request to another domain. http://stackoverflow.com/a/8449129/251311 – zerkms Nov 24 '12 at 01:15
  • 1
    When making an Ajax-request for that JSON file, the server response contains an `access-control-allow-origin: http://fiddle.jshell.net` HTTP-header, which means that the server uses CORS and returns the request's Origin, allowing all cross-domain requests. However, IE does not support CORS, but it does provide an alternative method do to cross-domain Ajax-requests. There are plug-ins that enable this feature in IE, e.g. https://gist.github.com/1114981 – Šime Vidas Nov 24 '12 at 01:20
  • @zerkms CORS is enabled on that particular server. It's just that jQuery does not provide CORS functionality for IE. Cross-domain Ajax-requests are, or course, possible, and OP's code works in non-IE browsers. – Šime Vidas Nov 24 '12 at 01:24
  • @Šime Vidas Thanks everyone! I will futz to see if this plugin is my solution. The plugin indicates IE8+. Does that mean IE7 is SOL? – chicgeek Nov 24 '12 at 02:21
  • @chicgeek Yes, but IE7 doesn't matter anymore. – Šime Vidas Nov 24 '12 at 03:00
  • @Šime Vidas As much as I believe that, some of my team might not. :/ Let's see if they notice! – chicgeek Nov 24 '12 at 03:27
  • @chicgeek IE7 has a share of 1% world wide. You site registers a larger share? – Šime Vidas Nov 24 '12 at 13:55
  • @Šime Vidas No, it's about half that, but unfortunately some of our stores have outdated computers on the floor with IE7. They should be replaced in the next few months, but it's something we need to consider. Thanks for your help - is there some way I can give kudos without you submitting an answer? (Or perhaps provide and answer with that plugin?) – chicgeek Nov 25 '12 at 04:50
  • 1
    IE 7 isn't even supported by MS anymore. Nor is IE8. Or IE9. Or IE10 =) The only still in support version of IE is IE11. Everything else very officially reached EOL _years_ ago. Of course, corporate or academic machines that still come with IE11 deserve support, but IE7 really, _really_ doesn't. Unless you're on [very exotic versions of Windows](https://learn.microsoft.com/en-us/lifecycle/faq/internet-explorer-microsoft-edge), which would definitely warrant mention in your post if you were. – Mike 'Pomax' Kamermans May 07 '21 at 00:42
  • Mike, you're commenting on questions and solutions that are nine years old. Of course many of these browser versions have since reached EoL. – chicgeek May 07 '21 at 06:24

1 Answers1

1

Due to browser security reasons, you cannot make requests from one origin to another (ex. jsfiddle.net to www.mec.ca). In recent years, browsers have started to circumvent this using CORS (Cross-Origin Resource Sharing). This allows browsers to make requests to another domain, as long as that server (www.mec.ca) is configured to accept them. You can check for this in the response header. For your url (https://www.mec.ca/api/v1/stores/1), CORS is enabled. You can see this in the response header on the line that says. Access-Control-Allow-Origin: *. This means requests are allowed from any domain (*)

HTTP/1.1 200 OK
Date: Sat, 24 Nov 2012 01:19:23 GMT
Server: Apache
Content-Language: en
Access-Control-Allow-Origin: *
X-Powered-By: Servlet/2.5 JSP/2.1
Vary: Accept-Encoding,User-Agent
Transfer-Encoding: chunked
Content-Type: application/json

Because IE has lagged behind in implementing the CORS standard, it was not added to IE until IE10. Therefore, your fiddle will not work in previous versions of IE.

Elmue
  • 7,602
  • 3
  • 47
  • 57
teddybeard
  • 1,964
  • 1
  • 12
  • 14
  • 1
    IE does provide an alternative method for doing cross-domain Ajax requests. It's just that jQuery doesn't make use of this method (but there are plug-ins which do). – Šime Vidas Nov 24 '12 at 01:27
  • yes, you are right. you can use `new XDomainRequest()` object. I edited my answer to say, the fiddle will not work instead of saying the cross-domain requests will not work. – teddybeard Nov 24 '12 at 01:42
  • However, I got this working on mec.ca on an https:// page with no further changes, but not http://. Does IE treat these protocols and different domains..? – chicgeek Nov 24 '12 at 04:19
  • not just IE, everything does: a different protocol means it's a different origin. – Mike 'Pomax' Kamermans May 07 '21 at 00:46