简体   繁体   中英

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. What would happen? Will the browser wait until the AJAX request is finished or it would depend on the internet speed of the user?

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.
  2. If you don't cancel the page-level submit event, the AJAX request will be sent and then the page will reload. In that case the AJAX request will be abandoned. 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. It'll change in every instance.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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