jQuery:$ .ajax成功回调不允许我分配给外部私有变量[重复]

时间:2022-05-12 19:55:25

This question already has an answer here:

这个问题在这里已有答案:

var doCheck = function() {
    var data;

    $.ajax({
        url: 'check.php',
        data: 'ch=check',
        success: function(resp) {
            data = resp;
        }
    });
console.log(data);
    return data == 1;
};

The above code, regardless of the data only ever returns a 0 or a 1. Within the scope of the success callback, this is true. The argument resp has a value of 0 or 1 depending on input.

上面的代码,无论数据只返回0或1.在成功回调的范围内,这是真的。参数resp的值为0或1,具体取决于输入。

However, whenever I try to access a private variable (should not be affected by scope), nothing happens; and when console.log(data); is called all that is written to the console is undefined.

但是,每当我尝试访问私有变量(不应受范围影响)时,都不会发生任何事情;当console.log(数据);调用写入控制台的所有内容都是未定义的。

This function has no parent, so don't worry about some other kind of scope interference.

此功能没有父级,因此不必担心其他类型的范围干扰。

5 个解决方案

#1


3  

Ajax is asynchronous. Which is why you have to organize your logic with callbacks:

Ajax是异步的。这就是为什么你必须用回调来组织你的逻辑:

var doCheck = function(callback) {
    var data;

    $.ajax({
        url: 'check.php',
        data: 'ch=check',
        success: function(resp) {
            callback(data == 1);
        }
    });
};

doCheck(function(result) {
    // result is true or false
});

#2


2  

Place the 'console.log(data)' sentence after 'data=resp'. Probably it is executing before the success method and becuase this it has no value set.

在'data = resp'之后放置'console.log(data)'句子。可能它在成功方法之前执行,并且因为它没有设置值。

#3


2  

It takes time till you get the response, so the console.log() comes when the data isn't set yet

获得响应需要一些时间,因此当数据尚未设置时,console.log()会出现

#4


0  

This is because the ajax Request by default is asynchronous, however instead of making it synchronous, I'd suggest redesigning you're approach.

这是因为默认情况下ajax请求是异步的,但是不建议它同步,我建议重新设计你的方法。

#5


0  

Ajax stands for Asynchronous JavaScript and XML. data is being set after the call to the server returns, and only if the call was sucessfull.

Ajax代表异步JavaScript和XML。在调用服务器返回后设置数据,并且仅在调用成功时才设置。

The console.log is happening immediatly after sending the async request off, before the sucess has a chance to run.

在成功发送之前,在发送异步请求之后,console.log立即发生。

#1


3  

Ajax is asynchronous. Which is why you have to organize your logic with callbacks:

Ajax是异步的。这就是为什么你必须用回调来组织你的逻辑:

var doCheck = function(callback) {
    var data;

    $.ajax({
        url: 'check.php',
        data: 'ch=check',
        success: function(resp) {
            callback(data == 1);
        }
    });
};

doCheck(function(result) {
    // result is true or false
});

#2


2  

Place the 'console.log(data)' sentence after 'data=resp'. Probably it is executing before the success method and becuase this it has no value set.

在'data = resp'之后放置'console.log(data)'句子。可能它在成功方法之前执行,并且因为它没有设置值。

#3


2  

It takes time till you get the response, so the console.log() comes when the data isn't set yet

获得响应需要一些时间,因此当数据尚未设置时,console.log()会出现

#4


0  

This is because the ajax Request by default is asynchronous, however instead of making it synchronous, I'd suggest redesigning you're approach.

这是因为默认情况下ajax请求是异步的,但是不建议它同步,我建议重新设计你的方法。

#5


0  

Ajax stands for Asynchronous JavaScript and XML. data is being set after the call to the server returns, and only if the call was sucessfull.

Ajax代表异步JavaScript和XML。在调用服务器返回后设置数据,并且仅在调用成功时才设置。

The console.log is happening immediatly after sending the async request off, before the sucess has a chance to run.

在成功发送之前,在发送异步请求之后,console.log立即发生。