繁体   English   中英

调用 ajax 到另一个 ajax

[英]Call ajax to another ajax

我有两个想要互相调用的 ajax

如果我的第一个 ajax 是:

 xhrAddressPoll = $.ajax({
    url: api,
    data: {
        address: address,
        longpoll: longpoll
    },
    dataType: 'json',
    cache: 'false',
    success: function(data){

        updateText('yourHashes', (data.stats.hashes || 0).toString());
        updateText('yourPaid', (data.stats.paid));

    },
    });

如果我的第二个 ajax 是:

  $.ajax({
      url: CoinPriceAPI,
      dataType: 'json',
      cache: 'false'
  }).done(function(data1) {
      coinPrice = data;
  updateText('coinPriceBTC', coinPrice.price );
  });

我的两个ajax如何相互调用?

我想要这样的输出示例:

updateText('yourPaidBTC', (data.stats.paid * coinPrice.price));

如果我喜欢这样:

 xhrAddressPoll = $.ajax({
    url: api,
    data: {
        address: address,
        longpoll: longpoll
    },
    dataType: 'json',
    cache: 'false',
    success: function(data){

        updateText('yourHashes', (data.stats.hashes || 0).toString());
        updateText('yourPaid', (data.stats.paid));
        updateText('yourPaidBTC', (data.stats.paid * coinPrice.price));

    },
    });

我有以下错误:

coinPrice 未定义

感谢您的帮助!

如果我正确理解你的问题,你想做这样的事情:

function ajax1() {

    $.ajax({
        ...
        success: function(ajax1Data) {
            ajax2(ajax1Data);
        }
    });
}

function ajax2(ajax1Data) {
    // call your second ajax here
}

至于您的“coinPrice 未定义”错误,听起来您处于严格模式(很好!)但您没有可以在函数之间共享的全局 coinPrice 变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM