Javascript 如何将ajax结果作为参数传递给另一个ajax以发出请求
我想将ajax结果id传递给另一个ajax,以便它可以调用另一个端点。我需要先得到基金的id和名称。然后,pass返回到另一个端点,以获取实际价格并显示给用户 因此,用户可以实时查看价格 我现在面临的挑战是,我能够显示姓名,但不能将身份证传回来Javascript 如何将ajax结果作为参数传递给另一个ajax以发出请求,javascript,ajax,loops,Javascript,Ajax,Loops,我想将ajax结果id传递给另一个ajax,以便它可以调用另一个端点。我需要先得到基金的id和名称。然后,pass返回到另一个端点,以获取实际价格并显示给用户 因此,用户可以实时查看价格 我现在面临的挑战是,我能够显示姓名,但不能将身份证传回来 <body> <div class="container"> <div class="text-center funds-item-container">
<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>
获得价格
$(文档).ready(函数(){
$.ajax({
键入:“GET”,
网址:'http://datarecapture.premiumpension.com:8089/api/Prices/GetAllFundNames',
contentType:“应用程序/json”,
成功:功能(resp){
log('hello:results',JSON.stringify(resp))
对于(变量i=0;i
`
);
}
},
错误:函数(xhr、状态、错误){
调试器
var errorMessage=xhr.status+':'+xhr.statusText
警报(“错误-”+错误消息);
}
});
$.ajax({
键入:“GET”,
网址:'http://datarecapture.premiumpension.com:8089/api/Prices/GetCurrentFundPrice?fundId='+${resp.resp.result[i].FUND_ID},
contentType:“应用程序/json”,
成功:功能(resp){
控制台日志(resp);
对于(变量i=0;i
`
);
}
},
//错误:函数(xhr、状态、错误){
//调试器
//var errorMessage=xhr.status+':'+xhr.statusText
//警报(“错误-”+错误消息);
})
});
您可以使用回调函数调用另一个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>
获得价格
$(文档).ready(函数(){
$.ajax({
键入:“GET”,
网址:'http://datarecapture.premiumpension.com:8089/api/Prices/GetAllFundNames',
contentType:“应用程序/json”,
成功:功能(resp){
log('hello:results',JSON.stringify(resp))
对于(变量i=0;i
`
);
}
},
错误:函数(xhr、状态、错误){
调试器
var errorMessage=xhr.status+':'+xhr.statusText
警报(“错误-”+错误消息);
}
});
});
函数callbackfun(resp)
{
$.ajax({
键入:“GET”,
网址:'http://datarecapture.premiumpension.com:8089/api/Prices/GetCurrentFundPrice?fundId="分别,,
contentType:“应用程序/json”,
成功:功能(resp){
控制台日志(resp);
对于(变量i=0;i
`
);
}
},
});
}
您可以将代码作为JavaScript函数写入一个单独的.js文件中,并从html页面调用该函数。然后在该函数中,您可以调用将执行其他ajax请求的其他函数。哇。谢谢。响应现在显示在控制台中。但我需要在它们的名称下显示每个单价。抱歉,我正在询问你可以创建一个以基金id为id的表格,并将数据推送到表格中。请阅读-总结是,这不是解决志愿者问题的理想方式,可能会对获得答案产生反作用。请不要将此添加到你的问题中。