Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Ajax to poll request status

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use Ajax to poll request status". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use Ajax to poll request status".

The function to be realized here is to log in to the website by scanning the official account of Wechat with a QR code with parameters.

However, it is obvious that if ajax continuously requests the server, this will increase the load on the server, so this example uses the setInterval of js to periodically call and execute an ajax function to request data from the server, but use the clearinterval function to clear the timer if the request is successful or does not succeed after a certain number of requests.

The code and comments are as follows: (the back end is implemented in thinkPHP, so the js code contains some thinkPHP syntax rules)

Var uid = "{$uid}"; var I = 0 uid var timer;$ (). Ready (function () {/ / Open the scan login modal box $('# login') .click (function () {/ / if the user is already logged in, return if (uid) {return;} / Open the mode box and load the data $('# loginModel'). Modal ({remote: "{: U ('user/login')}"}) remotely through the remote option;}) / / clear the data $("# loginModel"). On ("hidden.bs.modal", function () {$(this) .removeData ("bs.modal");}) after the modal box is hidden; / / when the modal box is displayed, request data from the server by returning to the server regularly. The timer is to request the server $('# loginModel'). On ('shown.bs.modal', function (e) {timer = setInterval (ajax_request, 3000) every three seconds (});}); / / ajax request function, function ajax_request () {idelay response response / if the request has not been successful for 20 seconds, it is forced to terminate and a prompt is given, because it is called every 3 s for 20 times, which is about one minute if (I > 20) {$('.login _ info1'). Html (' login timeout, refresh the page if you need to log in ~'); clearInterval (timer); return; } $.ajax ({type: "post", url: "{: U ('User/login_qrcode')}", timeout: 3000 scene_id' data: {"scene_id": $(' # scene_id'). Val ()}, success: function (msg) {if (1 = = msg.status) {$('.login _ info1'). Html (''+ msg.info+''); setTimeout (refresh, 3000); return;}}, error: function () {}) } / / reload page function refresh () {location.reload ();} above is all the content of the article "how to use Ajax to poll request status". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report