本文介绍了通过对AJAX传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些变数,我想传递到一个AJAX调用:
I have some variables that I would like to pass into an AJAX call:
例如。
var moo = "cow noise";
$.ajax({
type: "POST",
url: "",
data: "",
success: function(data){
//return the variable here
alert(moo);
}
});
不过,哞
回来不确定的。
注意的,我已经离开网址
和数据
空的目的 - 他们填充在我的code。
note, I've left url
and data
empty on purpose - they are populated in my code.
推荐答案
我猜你的code可能已经被包在 $(函数(){...});
作为一个jQuery的事情。删除 VAR
将使其基本上 window.moo =牛噪音;
哪些工作,但污染这是命名空间不是你想要的。
I guess your code might have been wrapped in $(function(){ ... });
as an jQuery thing. remove var
will make it basically window.moo = "cow noise";
Which works, but pollutes the namespaces which is not what you want.
不要污染全局命名空间,它会让你的另一code难以调试。使用封闭这应该解决您的问题:
Do not try to pollute the global namespace, it will make your other code hard to debug.Use closure which should solve your issue:
var moo = "cow noise";
(function(moo){
$.ajax({
type: "POST",
url: "",
data: "",
success: function(data){
//return the variable here
alert(moo);
}
});
})(moo);
这篇关于通过对AJAX传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!