简体   繁体   English

AJAX在提交按钮上的点击事件中

[英]AJAX in a click event on a submit button

I'd like to ask you guys what would happen if I use an AJAX request when an user click on a submit buttons that changes the page. 我想问你们,当用户单击更改页面的提交按钮时,如果我使用AJAX请求,会发生什么情况。 What would happen? 会发生什么? Will the browser wait until the AJAX request is finished or it would depend on the internet speed of the user? 浏览器会等待AJAX​​请求完成还是取决于用户的互联网速度?

Thanks. 谢谢。

One of two things: 两件事之一:

  1. If you cancel the page-level submit event and handle the form via AJAX, the page won't refresh and your code will be responsible for managing the state of the DOM in response to the AJAX request. 如果您取消页面级提交事件并通过AJAX处理表单,则页面将不会刷新,并且您的代码将负责管理DOM的状态以响应AJAX请求。
  2. If you don't cancel the page-level submit event, the AJAX request will be sent and then the page will reload. 如果您取消页面级提交事件,则将发送AJAX请求,然后重新加载页面。 In that case the AJAX request will be abandoned. 在这种情况下,AJAX请求将被放弃。 The server may or may not process it, the browser will ignore any response. 服务器可能会或可能不会对其进行处理,浏览器将忽略任何响应。

    it would depend on the internet speed of the user 这取决于用户的互联网速度

Never, ever rely on resource timing for managing asynchronous operations. 永远不要依赖资源定时来管理异步操作。 It's entirely unpredictable. 这是完全不可预测的。 Asynchronous operations are generally handled with various forms of continuations or callbacks in various language constructs which either wait on another thread for a response or get queued to interrupt the current thread for a response. 异步操作通常使用各种语言构造中的各种形式的延续或回调来处理,这些形式要么等待另一个线程进行响应,要么排队等待中断当前线程以进行响应。 Structure the logic accordingly. 相应地构建逻辑。 Relying on resource timing is like using a for loop with an operation of a known speed to measure the passage of time. 依靠资源定时就像使用for循环以已知速度操作来测量时间的流逝。 It'll change in every instance. 它会在每个实例中发生变化。

ajax请求是异步的,因此您将面临竞速问题,很可能在收到对ajax请求的响应之前,页面将发生更改,并且如果页面完全不同(不仅仅是显示的div),您将不会得到响应内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM