javascript - Passing variables through to AJAX -
i have variables pass ajax call:
e.g.
var moo = "cow noise"; $.ajax({ type: "post", url: "", data: "", success: function(data){ //return variable here alert(moo); } }); however, moo comes undefined.
note, i've left url , data empty on purpose - populated in code.
i guess code might have been wrapped in $(function(){ ... }); jquery thing. remove var make window.moo = "cow noise"; works, pollutes namespaces not want.
do not try pollute global namespace, make other code hard debug. use closure should solve issue:
var moo = "cow noise"; (function(moo){ $.ajax({ type: "post", url: "", data: "", success: function(data){ //return variable here alert(moo); } }); })(moo);
Comments
Post a Comment