javascript - 从 AJAX 调用中获取响应 - 接受回调

标签 javascript jquery ajax

我阅读了有关 AJAX 的信息并收到了来自它的调用,我正在尝试重组代码以开始工作。

处理接受回调的函数。

我创建了代码,但变量错误仍然无法在 AJAX 之外工作。

    $('.login').submit(function(e) {

        e.preventDefault(); 
        var error = 0;

    var email = $email.val();

    // function that makes a request and returns a promise:
    function testAjax() {
        return $.ajax({                                      
            url: 'MY URL',
            data: {
            'email': email
            },                        
            type: 'POST',
        });
    }

    // function that expects a promise as an argument:
    function displayData(x) {
        x.success(function(realData) {
            if (realData == 1) {
                createErrTult("Błąd! taki email już istnieje w bazie!", $email)

    //THAT VARIABLE IS STILL NOT WORKING OUTSIDE AJAX
                error++;
            }
        });
    }

    // get a promise from testAjax:
    var promise = testAjax();

    // give a promise to other function:
    displayData(promise);

            if (error!=0)return;
        self.find('[type=submit]').attr('disabled', 'disabled');

        self.children().fadeOut(300,function(){ $(this).remove() })
        $('<p class="login__title">sign in <br><span class="login-edition">welcome to A.Movie</span></p><p class="success">You have successfully<br> signed in!</p>').appendTo(self)
        .hide().delay(300).fadeIn();

});

我该如何解决这个问题?以为那段代码是正确的。

最佳答案

尝试

$('.login').submit(function(e) {

e.preventDefault(); 
var error = 0;

var email = $email.val();

// function that makes a request and returns a promise:
function testAjax() {
    return $.ajax({                                      
        url: 'MY URL',
        data: {
        'email': email
        },                        
        type: 'POST',
    });
}
var error = 0;
var promise = testAjax();
function displayData(x) {
   return x.then(function(realData) {
        if (realData == 1) {
            createErrTult("Błąd! taki email już istnieje w bazie!", $email)

//THAT VARIABLE IS STILL NOT WORKING OUTSIDE AJAX
            error++;
        }
    }, function(jqxhr, textStatus, errorThrown) {
       console.log(errorThrown)
    });
};
displayData(promise).then(function(data) {
     // if (error !=0 ) return;
     // evaluate `error` status here , 
     // do stuff
});

function test() {
  return $.ajax("https://gist.githubusercontent.com/guest271314/6a76aa9d2921350c9d53/raw/49fbc054731540fa68b565e398d3574fde7366e9/abc.txt")
};
var error = 0;
var promise = test();
function displayData(x) {
   return x.then(function(realData) {
       if (realData) {
            console.log("Błąd! taki email już istnieje w bazie!")

//THAT VARIABLE IS STILL NOT WORKING OUTSIDE AJAX
            error++;
        }
    }, function(jqxhr, textStatus, errorThrown) {
       console.log(errorThrown)
    });
};
displayData(promise).then(function(data) {
  if (error !== 0)
  console.log(error)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

关于javascript - 从 AJAX 调用中获取响应 - 接受回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27706760/

相关文章:

JQuery 测量隐藏元素的位置

javascript - 如何从异步调用返回响应?

asp.net-mvc - 自动映射器列表到 int(计数)

javascript - jQueryUI 滑动效果 - Divs 堆叠

javascript - 为什么我会收到 Uncaught TypeError : Cannot read property '0' of undefined on assign?

javascript - 如何在 MS Dynamics CRM Online 中单击按钮重定向到任何 Web 资源?

php - jquery伪造一个ajax请求

javascript - 点击事件不会在 jquery 中触发

php - 通过 cURL 发送 JSON 始终返回 "No access"

jQuery 将两个元素上的悬停视为一个元素