Tuesday, 15 June 2010

Jquery jsonp ajax callback not working -



Jquery jsonp ajax callback not working -

i working on sample illustration of implementing jsonp using jquery ajax .

can please explain why callback function 'customcb' not getting invoked in below illustration , confused.

from client application making $.ajax phone call asp.net mvc server application, below client code

$().ready(function () { $.ajax({ url: 'http://localhost:55632/', type: 'get', datatype: 'jsonp', jsonp: 'cbqs', jsonpcallback: 'customcb', success: function (data) { alert('success'); }, error: function (xmlhttprequest, textstatus, errorthrown) { alert(errorthrown); } }); function customcb() { alert('customcb'); } });

and below simple asp.net mvc action written in server application

public class homecontroller : controller { public string index() { emp x = new emp { fname = "first name", lname = "last name" }; string json = jsonconvert.serializeobject(x); response.contenttype = "application/x-javascript"; string str = "customcb(" + json + ")"; homecoming str; } .............. ..............

expected result: both 'success' , 'customcb' alert messages should displayed.

actual result : 'success' alert message getting displayed.

note : able access returned info success function , there no js errors.

please allow me know why 'customcb' alert message not getting displayed.

move jsonpcallback function out of "$().ready(function ()" code block.

function customcb() { alert('customcb'); }

jquery ajax jsonp

No comments:

Post a Comment