javascript - jQuery Ajax passing response variable to function -
i have javascript function uses jquery ajax fetch response servlet. works when use anonymous function method-
function doajax() { $.ajax({ url : 'authentication', data : { username : 'poko' }, success : function(data){ alert(data); //this works } }); }
but when try pass response variable declared function nothing happens.
function doajax() { $.ajax({ url : 'authentication', data : { username : 'poko' }, success : showresult(data) //this doesn't }); } function showresult(d) { alert(d); }
firefox debug gives referenceerror: data not defined. can second method work?
in second attempt
function doajax() { $.ajax({ url : 'authentication', data : { username : 'poko' }, success : showresult(data) //this doesn't }); }
you executing showresult
, assigning result success
handler. success
expects anonymous function or function reference(without passing parameters it).
what can is
function doajax() { $.ajax({ url : 'authentication', data : { username : 'poko' }, success : showresult }); }
and data
automatically passed in first parameter function referenced.
Comments
Post a Comment