Reputation: 2230
I must be missing something here, because this seems like a very simple problem, but I've tried most of the solutions and haven't had any luck.
I am simply trying to create an ajax request cross-domain and store the result into a javascrip variable. I'm getting valid javascript, my callback is being fired, but my success never is.
I've tried:
Setting jsonp: false
with jsonpCallback: myCallback
(as it stands now, below)
Remobing jsonp: false
and jsonpCallback: mcCallback
and setting the url url?callback=?
A few other things... point being, I'm doing something wrong and it's pretty basic
For the example below, I do get the alert('hello')
but then I get parsererror
in the console (coming from the error function)
Edit to add how it is now after reading a comment. Same issue, though. Sucess is not called
$.ajax({
url: 'http://localhost/faqservice/questions',
type: 'GET',
dataType: 'jsonp',
error: function (x, t, r) { alert(x + t + r); },
success: function (data) {
alert('success');
}
});
How the question was originally
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://localhost/faqservice/questions",
dataType: 'jsonp',
jsonp: false,
jsonpCallback: myCallback,
error: function (httpReq, status, exception) {
console.log(status);
},
success: function(data) {
alert('success');
}
});
});
myCallback = function (data) {
alert('hello');
}
Upvotes: 1
Views: 3891
Reputation: 647
As the notes said, your server must respond like 'myCallback({"some":"data"})'
not just the raw json: '{"some":"data"}'
jsonp is a bit strange, it's requested by inserting a script tag, cuz script tags CAN transfer cross-domain. Your server must supply JS code that'll do something when the response comes.
jQuery does most of the browser side for you. When it sends the request, it'll add on an argument like callback=jQuery17209078477467410266_1426708743857
Then it sets up a function of that name that'll take care of the response when it comes. Your server must take this string and construct the code like this: jQuery17209078477467410266_1426708743857({"data": "the"});
If you just pass the json, it'll execute like a big expression and be forgotten.
Upvotes: 1