Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback function for JSONP with jQuery AJAX

I didn't quite understand how to work with the callback for the ajax function of jQuery.

I have the following code in the JavaScript:

try {     $.ajax({         url: 'http://url.of.my.server/submit?callback=?',         cache: false,         type: 'POST',         data: $("#survey").serialize(),         dataType: "jsonp",         timeout: 200,         crossDomain: true,         jsonp: 'jsonp_callback',         success: function (data, status) {             mySurvey.closePopup();         },         error: function (xOptions, textStatus) {             mySurvey.closePopup();         }     }); } catch (err) {     mySurvey.closePopup(); } 

And on the server side (AppEngine / Python) I get the value of the callback parameter and respond with

self.response.headers['Content-Type'] = 'application/json; charset=utf-8' self.response.out.write(callback + '({"msg": "ok"});') 

But then I get an "Error: jQuery152042227689944248825_1317400799214 is not a function" in the browser console.

What is the proper way to handle this? Right now I get the results that I need, but the fact that I know it's not right is bothering me.

like image 948
Bani Avatar asked Sep 30 '11 17:09

Bani


People also ask

Can JSONP be used with AJAX?

JSONP allows you to sidestep the same-origin policy and to some extent make cross-domain Ajax calls. It's not a silver bullet, and it certainly has its issues, but in some cases it can prove invaluable when fetching data from a different origin.

What is JSONP callback function?

jsonp is the querystring parameter name that is defined to be acceptable by the server while the jsonpCallback is the javascript function name to be executed at the client. When you use such url: url: 'http://url.of.my.server/submit?callback=? ' the question mark ?

What is JSONP in AJAX?

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

What is callback function in AJAX?

Description. The ajaxSuccess( callback ) method attaches a function to be executed whenever an AJAX request completes successfully. This is an Ajax Event.


1 Answers

This is what I do on mine

$(document).ready(function() {   if ($('#userForm').valid()) {     var formData = $("#userForm").serializeArray();     $.ajax({       url: 'http://www.example.com/user/' + $('#Id').val() + '?callback=?',       type: "GET",       data: formData,       dataType: "jsonp",       jsonpCallback: "localJsonpCallback"     });   });  function localJsonpCallback(json) {   if (!json.Error) {     $('#resultForm').submit();   } else {     $('#loading').hide();     $('#userForm').show();     alert(json.Message);   } } 
like image 179
atbebtg Avatar answered Oct 10 '22 01:10

atbebtg