0

I am having problems using jquery to grab json data from a web service that lies on a different subdomain from where my client side code is. When I access the exact same json data from a local text file, my code works fine.

The json data is coming from this address

var jsonFeed = https://crm.bmw.ca/webservices/RetailerLocator.ashx?language=en&callback=?

The MIME type of the data is text/html, however I have also tried application/json.

Here is one method of access

$.getJSON(jsonFeed, function (data) {
    $.each(data, function (i, item) {
        alert(item);
    });
});

I've also tried this method, which came back with a parsererror. I've also tried this with a jsonp datatype

$.ajax(jsonFeed, {
    crossDomain: true,
    dataType: "json",
    success: function (data, text) {
        $.each(data, function (i, item) {
            alert(item);
        });
    },
    error: function (request, status, error) {
        alert(status + ", " + error);
    }
});

My code has to be entirely client side so a proxy isn't an option right now.

An example of someone with a very similar problem can be found here. jQuery AJAX JSON dataType Conversion

3 Answers 3

1

You can only work within the confines of what is possible. Same-origin policy can't be subverted, although you can use things like cross-domain policy headers on each of your servers to essentially link them together. However, that's only supported in the newer crop of browsers, and you have to control all the servers in the network.

See: http://en.wikipedia.org/wiki/Same_origin_policy for more information on what you're up against.

Sign up to request clarification or add additional context in comments.

Comments

0

While the returned JSON data should probably be of type text/json, the bigger problem is that the API call is not respecting your "callback" parameter. Since you're calling the API cross-domain you have to use JSONP which means your data should be returned inside of a function call. For example, if you navigate to https://crm.bmw.ca/webservices/RetailerLocator.ashx?language=en&callback=mycallback you should see something like this returned:

mycallback([{"RetailerID":1110,"Name":"BMW St. John's","Address":"120 Kenmount Road"...)

The fact that the callback function name specified in the "callback" argument isn't showing up as part of the returned data probably means that you're using an incorrect name for that parameter. Or, it could be that the system is not configured to allow cross-domain requests. You should contact the system admin and make sure the API allows cross-domain requests and also check the docs for that API and make sure you're using the correct callback parameter name.

3 Comments

Thanks Joe, I will send this onto the admins of that web service.
Well I don't know what they did, but the web service now responds to the JSONP Callback request properly. So my code was fine, just needed the server to respect the request.
They definitely resolved whatever was causing the JSON padding function to not be output. Good luck with the project!
0

So far as I can tell from playing with JSFiddle (http://jsfiddle.net/CEDB5/), the question/answer you mentioned is correct: unless crm.bmw.ca starts sending the correct MIME type you are stuck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.