jQuery ajax返回值 [英] jQuery ajax return value

查看:131
本文介绍了jQuery ajax返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从jquery ajax返回值 pinNumber ,以便我可以将其附加到ajax之外。这是我的代码

How can I return the value "pinNumber" from jquery ajax so I can append it outside of the ajax. Here is my code


var x = pinLast + 1;
    for(i=x;i<=pinMany;i++) {
        var i = x++;
        var cardNumber = i.toPrecision(8).split('.').reverse().join('');
        var pinNumber = '';

        jQuery.ajax({
            type: "POST",
            url: "data.php",
            data: "request_type=generator",
            async: false,
            success: function(msg){
                var pinNumber = msg;
                return pinNumber;
                //pin number should return
            }
        });

        jQuery('.pin_generated_table').append(cardNumber+' = '+pinNumber+'
'); // the variable pinNumber should be able to go here }

请问我是否你不明白.. ^^谢谢

Please ask me if you don't understand.. ^^ thanks

推荐答案

默认情况下,AJAX是异步的,你不能从回调中返回一个值进行同步调用,你几乎肯定不想这样做。

AJAX is asynchronous by default, you cannot return a value from the callback without making a synchronous call, which you almost certainly don't want to do.

你应该为成功提供一个真正的回调函数: handler,并将程序逻辑放在那里。

You should supply a real callback function to the success: handler, and put your program logic there.

这篇关于jQuery ajax返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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