javascript 可以进行不返回的 AJAX 调用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12518363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:24:55  来源:igfitidea点击:

Possible to make AJAX call that doesn't return?

javascriptjqueryajax

提问by Paul Tomblin

Is it possible to send an jQuery.ajax call or equivalent without any sort of response? I want to trigger something on the server as I leave the page with the onbeforeunload command, but it's not something I need to feedback to the client, so I just want to send off the command and not wait for a response.

是否可以在没有任何响应的情况下发送 jQuery.ajax 调用或等效调用?当我使用 onbeforeunload 命令离开页面时,我想在服务器上触发一些东西,但这不是我需要反馈给客户端的东西,所以我只想发送命令而不是等待响应。

Is that possible?

那可能吗?

回答by gdoron is supporting Monica

Every request has a response.Even if the server throws an error a response is coming back with the error.

每个请求都有一个响应。即使服务器抛出错误,响应也会返回错误。

You can ignore the response if you like to just don't add a successcallback.

如果您不想添加success回调,则可以忽略响应。

$.ajax({
    url: "theURL",
    data: theData
});

回答by origamifreak2

You could use the navigator sendBeacon API mentioned referenced in this stack overflow answeror directly linked to here.

您可以使用此堆栈溢出答案中提到的或直接链接到此处的导航器 sendBeacon API 。

From the description on the site, it asynchronously sends off some data and just checks if it was able to queue the data for sending or not. The unload handler shouldn't ignore the request like it might do with other asynchronous XMLHttpRequests.

从网站上的描述来看,它异步发送一些数据,只是检查它是否能够将数据排队等待发送。卸载处理程序不应像处理其他异步 XMLHttpRequest 那样忽略该请求。

navigator.sendBeacon(url, data) - The sendBeacon() method returns true if the user agent is able to successfully queue the data for transfer, Otherwise it returns false.

navigator.sendBeacon(url, data) - 如果用户代理能够成功地将数据排队等待传输,则 sendBeacon() 方法返回 true,否则返回 false。

...

...

ensuring that the data has been sent during the unloading of a document is something that has traditionally been difficult for developers, because user agents typically ignore asynchronous XMLHttpRequests made in an unload handler.

确保在卸载文档期间发送数据对于开发人员来说历来是困难的,因为用户代理通常会忽略在卸载处理程序中生成的异步 XMLHttpRequest。

回答by dm03514

If you are trying to return a response as soon as possible I think that is more of a server architecture decision. You can offload the actual work the request involves to a broker or something so you can return to the user as soon as possible? I don't think sending just a one way message is feasible

如果您想尽快返回响应,我认为这更多是服务器架构的决定。您可以将请求涉及的实际工作卸载给代理或其他东西,以便您可以尽快返回给用户?我不认为只发送一条消息是可行的

回答by hsnm

Assuming you're using something like this:

假设你正在使用这样的东西:

$.ajax({
   url: "test.html",
   context: document.body
})

(borrowed from http://api.jquery.com/jQuery.ajax/and edited), you can discard the response.

(借用http://api.jquery.com/jQuery.ajax/并编辑),您可以丢弃响应。