如何在jQuery/JavaScript中设置全局变量?

Sam*_*Sam 1 jquery

我有一个ajax函数:

    $.ajax({
        url: 'http://localhost/process.php',
        type: 'post',
        data: '',
        success: function(output) {

var animal = output

        }
    });
Run Code Online (Sandbox Code Playgroud)

我希望var animal全局设置,所以即使在ajax函数的成功回调之外,我也可以在页面的任何地方调用它.这该怎么做?

hun*_*ter 5

在任何函数或jQuery构造之外声明它

var animal = null;

$(function(){
    $.ajax({
        url: 'http://localhost/process.php',
        type: 'post',
        data: '',
        success: function(output) {
            animal = output
        }
    });
});
Run Code Online (Sandbox Code Playgroud)