如何将ajax结果作为参数传递给另一个ajax以发出请求 [英] How to pass ajax result as parameter to another ajax to make a request
问题描述
我想将ajax结果ID传递给另一个ajax,以便它可以调用另一个端点.我需要先获得该基金的编号和名称.然后传递回另一个端点以获取实际价格并显示给用户.
I want to pass ajax result id to another ajax so that it can call the other endpoint. I need to first get the id and name of the fund. then pass is back to another endpoint to get the actual price and display to users.
因此用户可以实时查看价格.
So users can see the price at real-time.
我已经能够显示名称而不将ID传递回去,这是我现在面临的挑战.
I have been able to display the names not to pass the id back is the challenge I am facing now.
<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函数编写在单独的.js文件中,然后从html页调用该函数.然后,在函数内部可以调用将执行其他ajax请求的其他函数.
You can write the code as JavaScript function in a separate .js file and call that function from the html page. Then inside the function you can call other functions that will perform other ajax requests.
这篇关于如何将ajax结果作为参数传递给另一个ajax以发出请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!