0

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

10
  • have you included the ajax scripts? Commented Feb 6, 2015 at 20:55
  • 1
    When you are saying that you are getting the desired result if you paste this link in your browser, could you please show this result here? Do you get a valid JSONP response? Also don't forget to replace the ? in the callback parameter with a meaningful function name when pasting the url in your browser. Commented Feb 6, 2015 at 20:55
  • Darin, the result in the browser is a simple number, in this case a 2. Source code shows the plain number, nothing else. Commented Feb 6, 2015 at 20:57
  • Chain on a fail method and see what the error is Commented Feb 6, 2015 at 20:57
  • Ramesh, I unfortunately cannot follow. Commented Feb 6, 2015 at 20:57

2 Answers 2

1

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/

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

Comments

0

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.

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.