服务器XHR在JS中收到了事件(Server XHR got received event in JS)

编程入门 行业动态 更新时间:2024-10-27 18:31:29
服务器XHR在JS中收到了事件(Server XHR got received event in JS)

情况:

目标API是外部的,不得以任何方式更改。 我有一个JS客户端,在客户端按钮点击时向API发送一些数据。 就像是

$.ajax({ type: "POST", url: "http://api.url/some_resource", data: {name: 'John', email: 'john@john.com'} }).done(function(msg) { if (msg === 'ok') { alert('Your Callback Request Sent!'); } else { alert('Error occurred. Please try later.');long } });

Situation:

Target API is external and may not be changed in any way. I have a JS client that sends some data to the API on client button click. Something like

$.ajax({ type: "POST", url: "http://api.url/some_resource", data: {name: 'John', email: 'john@john.com'} }).done(function(msg) { if (msg === 'ok') { alert('Your Callback Request Sent!'); } else { alert('Error occurred. Please try later.');long } });

When I send the request I have to wait to receive the response from the server to see if it was successful and notify the client if it was or was not.

The problem is that I the API is friggin slow and does not reply directly that it received everything and will process the request, but instead is trying to do all the job like sending email and only then replying. Sometimes it takes about 20s for response to be received (the XHR request response).

Is it possible (jQuery or pure JS) to fire something like "request reached server event" so I would be at least sure there were no connection problems?

最满意答案

是否可能触发“请求已到达服务器事件”之类的内容

不 - 客户不知道该事件。 它只知道a)何时发送了请求的最后一位,以及b)何时收到了响应的第一位。 两者之间没有任何东西可以等待。

XMLHttpRequest接口提供以下readyState信息:

Val State Description --------------------------------------------------------------------------------- 0 UNSENT open()has not been called yet. 1 OPENED send()has not been called yet. 2 HEADERS_RECEIVED send() has been called, and headers and status are available 3 LOADING Downloading; responseText holds partial data. 4 DONE The operation is complete.

所以我至少可以肯定没有连接问题?

好吧,如果客户端可以检测到连接问题,您会收到通知。

Is it possible to fire something like "request reached server event"

No - the client does not know of that event. It only knows a) when it has sent the last bit of the request and b) when it has received the first bit of the response. There is nothing in between but waiting.

To the XMLHttpRequest interface there is the following readyState information available:

Val State Description --------------------------------------------------------------------------------- 0 UNSENT open()has not been called yet. 1 OPENED send()has not been called yet. 2 HEADERS_RECEIVED send() has been called, and headers and status are available 3 LOADING Downloading; responseText holds partial data. 4 DONE The operation is complete.

so I would be at least sure there were no connection problems?

Well, if there had been connection problems that are detectable to the client, you would've been notified of them.

更多推荐

本文发布于:2023-08-01 01:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收到了   事件   服务器   XHR   event

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!