[英]How do i get HTTP status code 200 instead of 202 from an ajax call
The below code always receives a HTTP '202 Accepted' status code:下面的代码总是收到一个 HTTP '202 Accepted' 状态码:
When I try the same API using postman I get 200 OK":当我使用 postman 尝试相同的 API 时,我得到 200 OK”:
function ExportStatus() {
function ExportStatusAjax() {
$.ajax({
type: "GET",
url: "https://api.powerbi.com/v1.0/myorg/groups/" + groupId + "/reports/" + reportId + "/exports/" + exportId + "",
headers: {
"Authorization": "Bearer " + accessToken
},
success: function(data, textStatus, jqXHR) {
if (jqXHR.status == 200) {
GetExportFile()
} else {
ExportStatusAjax()
}
},
error: function(err) {
alert(error)
}
});
}
ExportStatusAjax()
}
You have a recursion that will never end.你有一个永远不会结束的递归。 ExportStatusAjax() calls itself in 2 places at least.
ExportStatusAjax() 至少在 2 个地方调用自己。 Remove it from your code at all.
完全从您的代码中删除它。 And don' t pay to much attention to the status codes if there are not 400x or 500x.
如果没有 400x 或 500x,请不要太在意状态码。 As soon as it returns to success you are ready to continue.
一旦它恢复成功,您就可以继续。
I am not familiar with Power BI, so I can't say for sure, but judging by your code and the API status codes, this looks like a case of an asynchronous API.我不熟悉 Power BI,所以我不能肯定地说,但从你的代码和 API 状态代码来看,这看起来像是一个异步 API 的情况。 What that means is, instead of the traditional model in HTTP where the client submits a request and gets the response back (this is a 200, aka
OK
), a client submits an HTTP request and the response is a placeholder to tell you that the real response is not ready yet.这意味着,客户端提交请求并返回响应(这是一个 200,又名
OK
),而不是 HTTP 中的传统 model,客户端提交一个 Z293C9EA246FF9785DC6F62A6 的占位符,告诉您请求和 86真正的反应还没有准备好。
202
is called Accepted
and I believe that in this situation it is being used to indicate your request has been accepted into a queue. 202
被称为已Accepted
,我相信在这种情况下,它被用来表示您的请求已被接受到队列中。 The idea being that, if you keep retrying, eventually it will be done and you'll get the real response, which comes with a status code of 200
.这个想法是,如果你继续重试,最终它会完成并且你会得到真正的响应,它带有状态码
200
。 Reports, analytics, or other sorts of computationally expensive workloads often follow this pattern.报告、分析或其他类型的计算成本高昂的工作负载通常遵循这种模式。
This approach is a common pattern.这种方法是一种常见的模式。 If that is in fact what is happening here, then your code is probably correct and seeing the response flip between
202
and 200
is normal.如果这实际上是这里发生的事情,那么您的代码可能是正确的,并且看到响应在
202
和200
之间翻转是正常的。
With that said, your approach is constantly hammering the server.话虽如此,您的方法是不断地锤击服务器。 As soon as it gets back a
202
, it immediately retries, which adds load to the remote system, which theoretically may actually slow it down.一旦它返回
202
,它就会立即重试,这会增加远程系统的负载,理论上这实际上可能会减慢它的速度。
I suggest putting in a timer to delay the retries, maybe every 3 seconds or 5 seconds or so.我建议放置一个计时器来延迟重试,可能每 3 秒或 5 秒左右。 The
setTimeout
function can help you. setTimeout
function 可以帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.