如何将变量从ajax函数传递到另一个ajax函数? [英] How to pass variable from ajax function to another ajax function?

查看:226
本文介绍了如何将变量从ajax函数传递到另一个ajax函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将值从ajax函数传递到另一个ajax,我已经尝试过此代码,但是它不起作用.有人知道该怎么做吗?谢谢!

I want to know how to pass value from ajax function to another ajax I've tried this code but It does not work. Anyone know how to do it the right way? Thanks!

    $("#button").click(function(){
    var passedValue = '';
    $.ajax({
        url://
        type://
        success:function(data){
            if(typeof==="object"){
            for(var i=0;i<data.length;i++){
                passedValue = data[i]["passedThis"];
            }
        }
    }
    });
    $.ajax({
        data: { receivedPassed: passedValue;}
    });
});

推荐答案

那些请求是异步的,因此在发送第二个请求时passedValue尚未准备就绪.您可能需要按顺序发送它们:

Those requests are asynchronous, so passedValue isn't ready by the time the second one is sent. You'll probably want to send them in order:

$("#button").click(function() {
    $.ajax({
        url: //
        type: //
        success: function(data) {
            var passedValue;

            if(typeof (?) === "object") {
                for(var i = 0; i < data.length; i++) {
                    passedValue = data[i]["passedThis"];
                }

                $.ajax({
                    data: { receivedPassed: passedValue }
                });
            }
        }
    });
});

此外,您每次通过该循环都覆盖passedValue.您是说+=而不是=,还是使用数组或其他东西?

Also, you're overwriting passedValue each time through that loop. Did you mean += instead of =, or to use an array, or something?

这篇关于如何将变量从ajax函数传递到另一个ajax函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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