我正在尝试按顺序进行多个提取调用。因此,第一个调用工作得很好,但是,当我尝试在第一个调用后进行第二个调用时,它会抛出 500 错误。我一整天都在试图搞乱客户端和服务器端,但没有任何线索。所以我下面要做的是按顺序进行两次 fetch 调用,只是使用不同的方法和不同的 url。
首次调用(POST)有效
async func1(){
try {
// debugger;
await fetch('http://apiDomain/Ready', {
method: 'POST',
}).then((rsp) => {
if(rsp.ok) {
console.log(rsp.json());
this.func2();
}
});
}
catch (error) {
console.error(error);
alert("Error: ", error);
}
因此,当 func1 返回 rsp.ok 时,我会调用另一个调用(GET),func2。
async func2(){
try {
// debugger;
await fetch('http://apiDomain/ReadySetGo', {
method: 'GET',
}).then((rsp) => {
if (rsp.ok) {
console.log(rsp.json());
}
});
}
catch (error) {
console.error(error);
alert("Error: ", error);
}
(apiDomain 是一个 .asp.net 应用程序,在开发服务器中的 IIS 下运行) 我还在 func1 (then()) 之后独立调用了 func2 ,而不是嵌套的,还尝试了 Promise.all 来调用这两个函数,但无论我做什么,一旦第一个调用返回,第二个调用(不仅是 ReadySetGo,还包括任何 url) )只会抛出 500(内部服务器错误 - IIS 日志中发生错误)。
不太清楚为什么第一个返回 200 很好,但第二个总是失败。 任何帮助将不胜感激。
你可以使用链接
fetch('http://apiDomain/Ready', { method: 'POST' })
.then(response => {
// Check if the response is ok (status in the range 200-299)
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); // Parse JSON data from the response
})
.then(data => {
// Use data from the first request
const something = data.something;
return fetch('http://apiDomain/ReadySetGo', { method: 'GET' });
})
.then(response => {
// Check the second response is ok (status in the range 200-299)
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); // Parse JSON data from the second response
})
.then(details => {
console.log({ details }); // Handle the data from the second request
})
.catch(error => {
// Handle any errors that occurred during any of the fetch operations
console.error('Failed to fetch data:', error);
});