jsonp callback function not getting called with jQuery
I have scoured this site and elsewhere trying to solve the problem I am
having with jsonp. To start things off, here's the code that I have:
url = "http://mydomain.com/return_json";
$.ajax({
url: url, // + '?callback=?' --I realized this is not necessary with
dataType: 'jsonp'
dataType: 'jsonp',
crossDomain: true,
error: function(xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
},
success: function(dataWeGotViaJsonp){
var text = '';
var len = dataWeGotViaJsonp.length;
for(var i=0;i<len;i++){
item = dataWeGotViaJsonp[i];
text += '<p>' + item + '</p>';
}
$('#action_target').html(text);
}
});
On the sending side, the /return_json url is a Django site that is sending
json data the following way:
def return_json(request):
data = [{'testing': 'testing'}, {'one': 1, 'two': 2, 'three': 3}]
return HttpResponse( json.dumps(data),
content_type="application/javascript" )
As you can see in the JavaScript, I'm indescriminately dumping everything
into the console on error. Here is the output of that:
Object { readyState=4, status=200, statusText="success"}
parsererror
Error: jQuery110207276483389928793_1377030169256 was not called
The 'net' area of firebug shows that the url was:
http://mydomain.com/return_json?
callback=jQuery110209170565296948737_1377029879665&_=1377029879666
It also shows that valid JSON is in the response. It even has a JSON
section with a pretty-fied output. So, obviously my problem is that the
jQuery auto-generated callback function is there, but not getting called.
I get the same result using the $.ajax and $.getJSON methods set up for
jsonp. The only thing I can think of at this point is that I'm supposed to
wrap the json data in a function somehow on the sender's side, but I was
under the impression that the receiver takes care of that. If anyone can
see what I'm doing wrong, it would be much appreciated.
No comments:
Post a Comment