gpt4 book ai didi

javascript - HttpRequest 不会在冗长的请求上超时

转载 作者:行者123 更新时间:2023-11-30 14:58:11 27 4
gpt4 key购买 nike

我有以下 Ajax 调用

function exampleAjax() {
$('#loadingImage').show();

var id = GetId();
$.ajax({
url: "../Controller/MyRequest?id=" + id,
type: "GET",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (result) {
if (result.success === true) {
// Show Dialog Success
}
else {
// Show Dialog Failure
}
},
async: true,
processData: false
});
}

调用下面 Controller 的方法。

public async Task<JsonResult> MyRequest(string teamId)
{
bool success = false;
try
{
HttpResponseMessage response = await client.GetAsync(fullUriEndpoint)
if (!response.IsSuccessStatusCode)
throw new Exception(response.ReasonPhrase);
else
success = true;

return Json(new
{
success = success,
}, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new
{
success = success,
}, JsonRequestBehavior.AllowGet);
}
}

因为请求需要几分钟来处理(这完全没问题,服务器需要做一些可能需要 5 分钟或更长时间的处理)然后客户端发回一个异常,尽管任务仍在后端执行.

有没有一种方法可以解决这个问题,而不用一劳永逸?我想等待此请求的执行,直到它完成。

最佳答案

作为一种选择,您可以构建一种轮询过程:

  1. 客户端发送“长操作”请求。
  2. 服务器开始操作,给它一些id。
  3. 并立即回复客户已开始操作。
  4. 然后服务器监听 GET/api/operation/:id 之类的 API,以响应给定操作的状态(已完成或正在进行)。
  5. 客户从 (3) 得到这个预回答并运行一个计时器,他从 (4) 请求 API 每隔 500 毫秒,直到操作完成或直到完成 N 次尝试。

另一种选择是使用 WebSockets 在服务器和客户端之间提供双向通信。 .

关于javascript - HttpRequest 不会在冗长的请求上超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46920226/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com