简体   繁体   中英

getJSON callback not executing

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

$.getJSON("http://usr:pwd@10.0.1.xx/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, ie 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

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM