jQuery的阿贾克斯如何工作的回调? [英] jQuery Ajax How do callbacks work?

查看:309
本文介绍了jQuery的阿贾克斯如何工作的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好老乡程序员!我刚开始一个额外的编程项目,并发誓神就是我的code将博这么多清洁,易于升级比过去之前。

Hello fellow programmers! I just started an additional programming project and swore to god my code will bo SO much cleaner and easily upgradeable than it has been before.

然后,我偶然发现了我的头号敌人jQuery的AJAX返回。上次我想从一个AJAX调用我只好蹲下来,只是拨打电话同步返回的东西。这使事情变得粘稠,丑陋,我希望这一次我会找到更好的东西。

Then I stumbled upon my "arch enemy" the jQuery AJAX returning. Last time I wanted to return something from an AJAX call I had to bend over and just make the call synchronous. That made things sticky and ugly and I hope that this time I will find something better.

所以,我一直在谷歌上搜索/搜索计算器有一段时间了,只是不明白这个解决方案多少脂肪酶已经得到了这就是所谓的的回调函数的。可能有人给我一个例子,我怎么可以利用这些的的回调函数的,以回报我的登录状态:

So I have been googling/searching stackoverflow for a while now, and just don't understand this solution many ppl has gotten which is called callback function. Could someone give me an example on how I could exploit these callback functions in order to return my login statuses:

function doLogin(username, password) {
    $.ajax({
        url:        'jose.php?do=login&user='+username+'&pass='+password,
        dataType:   'json',
        success:    function(data) {
                        if(data.success==1) {
                            return('1');
                        } else {
                            return('2');
                        }
                        $('#spinner').hide();
                    },
        statusCode: {
                        403:function() {
                            LogStatus('Slavefile error: Forbidden. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        },
                        404:function() {
                            LogStatus('Slavefile was not found. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        },
                        500:function() {
                            LogStatus('Slavefile error: Internal server error. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        },
                        501:function() {
                            LogStatus('Slavefile error: Not implemented. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        }
                    },
        async:      true
    });
}

所以,你可能知道,你不能使用返回我从一个AJAX调用内部完成的方式。您应该使用的的回调函数的,我不知道如何使用。

So as you probably know, you cannot use return the way I have done from inside an AJAX call. You should instead use callback functions which I have no idea of how to use.

我会很感激,如果有人可以用我写这code的的回调函数的和向我解释只是它们如何工作。

I'd be VERY greatful if someone could write me this code using callback functions and explaining to me just HOW they WORK.

编辑:

我真的需要返回的东西,而不是使用它的时候了。此功能被称为从内的另一个功能,并应能够从不同的地方称为而不被重写甚至略有

I REALLY need to return stuff, not use it right away. This function is being called from within another function and should be able to be called from different places without being rewritten even slightly.

/编辑

诚恳,

Akke

Web开发的Oy公司艾莫拉特瓦拉抗体

Web Developer at Oy Aimo Latvala Ab

推荐答案

有三部分基本的我需要一个异步回调的格局:

There are three parts to the basic "I need an asynchronous callback" pattern:

  1. 提供的功能的回调函数的参数。
  2. 在调用返回一个值的回调函数。
  3. 而不是调用的函数,做一些与它的返回值,在返回值的将被传递给你的回调函数作为参数。<​​/ li>
  1. Give the function a callback function parameter.
  2. Call the callback function instead of returning a value.
  3. Instead of calling the function and doing something with its return value, the return value will be passed to your callback function as a parameter.

假设你同步的头脑要做到这一点:

Suppose your synchronous mind wants to do this:

function doLogin(username, password) {
    // ...
    return something;
}

switch(doLogin(u, p)) {
case '1':
    //...
    break;
case '2':
    //...
    break;
//...
}

doLogin 具有对远程服务器的异步调用。你只需要重新安排这样的事情一点:

but doLogin has to make an asynchronous call to a remote server. You'd just need to rearrange things a little bit like this:

function doLogin(username, password, callback) {
    return $.ajax({
        // ...
        success: function(data) {
            if(data.success == 1)
                callback('1');
            else
                callback('2');
        },
        //...
    });
}

var jqxhr = doLogin(u, p, function(statusCode) {
    switch(statusCode)) {
    case '1':
        //...
        break;
    case '2':
        //...
        break;
    //...
    }
});

借助 jqxhr 让你引用AJAX连接之前返回,你会使用它,如果你需要取消呼叫,附加额外的处理程序等。

The jqxhr allows you to reference the AJAX connection before it returns, you'd use it if you needed to cancel the call, attach extra handlers, etc.

这篇关于jQuery的阿贾克斯如何工作的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆