LFS
LFS

Reputation: 61

getJSON callback not executing

I need to do a cross domain request, and I have a problem with getJSON.

$.getJSON("http://usr:[email protected]/cgi-bin/remote/request.cgi?m=json&r=grp&fn=getvalue&alias=1/3/51&callback=?",
        function(result) {
            alert('hi');
        });

The callback does not fire, i.e. I do not get the alert. If I paste the http:// link in a browser window, I get the desired result. It is a plain number, in this case 2. I could also set the request to xml, in which case the result in the browser window is <value>2</value>. If someone could tell me what's wrong with my getJSON that would be great.

Many thanks and best regards

Upvotes: 0

Views: 1259

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

It appears that the remote endpoint you are trying to call doesn't support JSONP or CORS. This can be seen in the console output of your browser or by pasting the url directly and replacing the &callback=? parameter with &callback=somevalue.

In order to be able to make cross domain AJAX calls the remote endpoint must support CORS or JSONP. You should probably contact the authors of this endpoint for enabling one of those if you want to be able to consume them with AJAX.

Upvotes: 0

silverfighter
silverfighter

Reputation: 6882

You might have an issue with your JSON. This is taken from the docs.

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

source:

http://api.jquery.com/jquery.getjson/

to have more control over error handling use $.ajax

you can check your JSON here http://jsonlint.com/

Upvotes: 1

Related Questions