javascript - jsCall return value to outside of jquery ajax post request -
i want jquery ajax post request value outside ajax function. code , return undefined
console output. how should fix it
function submit() { var outputfromajax = submitviapost('administrator/validationforinputvaluesofaddrole'); console.log(outputfromajax); } function submitviapost(url) { var formdata = $('form').serializearray(); var output; $.post(urlforphp + '/' + url, formdata, function (outputdata) { output = outputdata; }); return output; }
edited
i changed code sync type ajax post request , check output. not changed. here code
function submit() { var outputfromajax = submitviapost('administrator/validationforinputvaluesofaddrole'); console.log(outputfromajax); } function submitviapost(url) { var formdata = $('form').serializearray(); var output; $.ajax({ url: urlforphp + '/' + url, data: formdata, datatype: 'json', async: false, method: 'post', success: function (e) { output = e; } }); return output; }
you can use deferred jquery
function submit() { submitviapost('administrator/validationforinputvaluesofaddrole').then(function (outputfromajax) { console.log(outputfromajax); }); } function submitviapost(url) { var dfd = jquery.deferred(); var formdata = $('form').serializearray(); $.post(urlforphp + '/' + url, formdata, function (outputdata) { dfd.resolve(outputdata);; }); return dfd; }
Comments
Post a Comment