How to implement encapsulation in Ajax
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)