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 encapsulate Ajax of javascript

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

Share

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

This article mainly explains "how to encapsulate the Ajax of javascript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to encapsulate the Ajax of javascript".

Basic package of ajax

Function ajax (url,fn) {

If (window.XMLHttpRequest) {var xhr = new XMLHttpRequest ();} else {var xhr = new ActiveXObject ("Microsoft.XMLHTTP");} xhr.open ("get", url,true); xhr.send (); xhr.onreadystatechange = function () {if (xhr.readyState = = 4) {if (xhr.status = = xhr.responseText; fn (data);}

Complete package of ajax

Function ajax (obj) {

/ / obj-> type url data successvar str = ""; for (var key in obj.data) {str + = key+ "=" + obj.data [key] + "&";} / / str = str.substring; str = str.replace (/ & $/, "); if (window.XMLHttpRequest) {var xhr = new XMLHttpRequest ();} else {var xhr = new ActiveXObject (" Microsoft.XMLHTTP ") } if (obj.type.toUpperCase () = = "GET") {if (obj.data) {var url = obj.url + "?" + str;} else {var url = obj.url;} xhr.open ("get", url,true); xhr.send ();} if (obj.type.toUpperCase () = = "POST") {xhr.open ("post", obj.url,true) Xhr.setRequestHeader ("Content-type", "application/x-www-form-urlencoded"); xhr.send (str);} Thank you for reading, the above is the content of "how to encapsulate the Ajax of javascript". After the study of this article, I believe you have a deeper understanding of how to encapsulate the Ajax of javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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