【问题标题】:How to pass ajax result as parameter to another ajax to make a request如何将 ajax 结果作为参数传递给另一个 ajax 以发出请求
【发布时间】:2019-09-14 10:15:27
【问题描述】:

我想将 ajax 结果 id 传递给另一个 ajax,以便它可以调用另一个端点。我需要先获取基金的 ID 和名称。然后传回另一个端点以获取实际价格并显示给用户。

因此用户可以实时查看价格。

我已经能够显示名称而不将 id 传回是我现在面临的挑战。


 <body>
        <div class="container">
            <div class="text-center funds-item-container">
                <h3>Get Prices</h3>
            </div>
        </div>

        <script>
        $(document).ready(function () {
        $.ajax({  
            type: 'GET',
            url: 'http://datarecapture.premiumpension.com:8089/api/Prices/GetAllFundNames',
            contentType: "application/json",
            success: function(resp) {
            console.log('hello: results', JSON.stringify(resp))


              for (var i = 0; i < resp.result.length; i++) {
                console.log('hello: results', JSON.stringify(resp.result[i].FUND_NAME));


                $('.funds-item-container').append(
                   `<div><h5>Price ${i+1}</h5>
                      <a id='#price${i+1}'>
                       <a href="${resp.result[i].FUND_ID}"> ${resp.result[i].FUND_NAME}</a>
                      </p>
                    </div><hr>`
                );
              }
            },

            error: function(xhr, status, error){
              debugger
               var errorMessage = xhr.status + ': ' + xhr.statusText
               alert('Error - ' + errorMessage);
            }
    });




    $.ajax({  
            type: 'GET',
            url: 'http://datarecapture.premiumpension.com:8089/api/Prices/GetCurrentFundPrice?fundId=' + ${resp.result[i].FUND_ID} ,
            contentType: "application/json",
            success: function(resp) {
              console.log(resp);


             for (var i = 0; i < resp.result.length; i++) {
               //console.log('hello: results', JSON.stringify(resp.result[i].FUND_NAME));


                $('.funds-item-container').append(
                   `<div><h3>'</h3>
                      <a id='#price${i+1}'>
                       ${resp.result[i].FUND}
                      </p>
                    </div><hr>`
                );
          }
            },

           // error: function(xhr, status, error){
           //   debugger
           //    var errorMessage = xhr.status + ': ' + xhr.statusText
           //    alert('Error - ' + errorMessage);
            })
    });


 </script>
        </body>

【问题讨论】:

标签: javascript ajax loops


【解决方案1】:

您可以将代码编写为单独的 .js 文件中的 JavaScript 函数,然后从 html 页面调用该函数。然后在函数内部,您可以调用将执行其他 ajax 请求的其他函数。

【讨论】:

    【解决方案2】:

    您可以使用回调函数调用另一个 API 端点

    <body>
        <div class="container">
            <div class="text-center funds-item-container">
                <h3>Get Prices</h3>
            </div>
        </div>
    
        <script>
        $(document).ready(function () {
        $.ajax({  
            type: 'GET',
            url: 'http://datarecapture.premiumpension.com:8089/api/Prices/GetAllFundNames',
            contentType: "application/json",
            success: function(resp) {
            console.log('hello: results', JSON.stringify(resp))
    
    
              for (var i = 0; i < resp.result.length; i++) {
                console.log('hello: results', JSON.stringify(resp.result[i].FUND_NAME));
                    callbackfun(resp.result[i].FUND_ID)
    
                $('.funds-item-container').append(
                   `<div><h5>Price ${i+1}</h5>
                      <a id='#price${i+1}'>
                       <a href="${resp.result[i].FUND_ID}"> ${resp.result[i].FUND_NAME}</a>
                      </p>
                    </div><hr>`
                );
              }
            },
    
            error: function(xhr, status, error){
              debugger
               var errorMessage = xhr.status + ': ' + xhr.statusText
               alert('Error - ' + errorMessage);
            }
    });
    
    
    
    
    });
    
     function callbackfun(resp)
    {
        $.ajax({  
                type: 'GET',
                url: 'http://datarecapture.premiumpension.com:8089/api/Prices/GetCurrentFundPrice?fundId=' +resp ,
                contentType: "application/json",
                success: function(resp) {
                  console.log(resp);
    
                 for (var i = 0; i < resp.result.length; i++) {
                   //console.log('hello: results', JSON.stringify(resp.result[i].FUND_NAME));
    
    
                    $('.funds-item-container').append(
                       `<div><h3>'</h3>
                          <a id='#price${i+1}'>
                           ${resp.result[i].FUND}
                          </p>
                        </div><hr>`
                    );
              }
    },
    
                });
    }
    
    </script>
        </body>
    

    【讨论】:

    • 哇。谢谢。响应现在显示在控制台中。但我需要在他们的名字下显示每个单价。对不起,我问这个
    • 您可以创建一个以fund id为id的表并将数据推入其中
    猜你喜欢
    • 2015-09-21
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2015-07-08
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多