通过对AJAX传递变量变量、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...

我有一些变数,我想传递到一个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);
阅读全文

相关推荐

最新文章