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.
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.
Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Dec 18, 2013 at 12:47 Ranveer Singh RajpurohitRanveer Singh Rajpurohit 6813 gold badges13 silver badges28 bronze badges 9-
3
the response is not a valid jsonp response.... it should be something like
jsonpCallbackName({"Hello":"World"})
– Arun P Johny Commented Dec 18, 2013 at 12:48 - @ArunPJohny I don't have control on response, It is hosted by third party. I can only just consume it :( – Ranveer Singh Rajpurohit Commented Dec 18, 2013 at 12:52
-
Why do you specify
jsonpCallback
and also success function which gets your json ? – Royi Namir Commented Dec 18, 2013 at 12:52 -
2
And please, dont set
async: false
. You're locking the browser with no reason. – Denys Séguret Commented Dec 18, 2013 at 12:53 - 1 "It is hosted by third party" so does it accept jsonp request? – A. Wolff Commented Dec 18, 2013 at 12:55
2 Answers
Reset to default 3If 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);
}
});
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