1

I am using below code to access rest service hosted on another domain.

$.ajax({
            type: 'GET',
            url: url,
            async: false,
            jsonpCallback: 'jsonCallback',
            contentType: "application/json",
            dataType:"jsonp",
            success: function(json) {
                alert(json);
            },
            error: function(e) {
               console.log(e.message);
            }
        });

I am able to get the data correctly, but I get this error in firebug in mozilla:

SyntaxError: missing ; before statement

{"Hello":"World"}

Can anyone suggest me what I am doing wrong here? Even though Json data is valid. I tried all the suggestions posted in this question But still I am getting same error.

Community
  • 1
  • 1

2 Answers2

3

If it's really JSON you ask for, don't set "jsonp" as dataType, and don't provide a callback :

$.ajax({
        type: 'GET',
        url: url,
        contentType: "application/json",
        success: function(json) {
            alert(json);
        },
        error: function(e) {
           console.log(e.message);
        }
});
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

the format of JSON and JSONP are slightæy different JKSONP is a function invocation expression

callback({"hellow":"world"});

whereas JSON is simply a serialized object

{"Hello":"world"}

fromyour posting it would seem that the server is returning JSON and not JSONP

So you either need to change the server to reply correctly (The actual callback name is a get parameter to the request). You should do this if you are using the ajax call across domains

If you are not using ajax across domains stick to regular JSON

Rune FS
  • 21,497
  • 7
  • 62
  • 96