Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement encapsulation in Ajax

Shulou Source: shulou.com Published: 2022-06-03 14:33:55 10月03日 Update

Editor to share with you how to achieve Ajax encapsulation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

In the past, it was to click the button to take out the news, but now it is necessary to update the news every other event. At this time, you must add a timer, and then make another Ajax request every other event. Since the Ajax request is often used, it is necessary to encapsulate the function.

_ window.onload = function () {var oBtn = document.getElementById ('btn'); oBtn.onclick = function () {var xhr = null;if (window.XMLHttpRequest) {xhr = new XMLHttpRequest ();} else {xhr = new ActiveXObject (' Microsoft.XMLHTTP');} xhr.open ('get','getNews.php',true); xhr.send () Xhr.onreadystatechange = function () {if (xhr.readyState = = 4) {if (xhr.status = = 200) {/ / tasks executed after success are marked in red var data = JSON.parse (xhr.responseText); / / convert the content acquired in the background to json type there are two keys in each json: title and datevar oUl = document.getElementById ('ul1'); / / get the node showing the news list var html ='; for (var item0; I)

Tags: Functions that is parameters encapsulation data content things news ways articles successes events times changes different necessary less two things tasks Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi NVidia OPPO Reno Shulou Technology