ajax封装

 2023-09-10 阅读 19 评论 0

摘要://接收文本:$$.init({url : "test.php", type : "post", data : "name=john", success : function(result){}});//接收XML:$$.init({url : "test.xml", type : "get", success : function(result){result.getElem
			//接收文本:$$.init({url : "test.php", type : "post", data : "name=john", success : function(result){}});//接收XML:$$.init({url : "test.xml", type : "get", success : function(result){result.getElementsByTagName()}});var $$ = {init : function(p){this.parameter = p;this.xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();this.type = (this.parameter.type === "post" || this.parameter.type === "POST") ? "POST" : ( (this.parameter.type === "get" || this.parameter.type === "GET") ? "GET" : "POST" );this.dataType = this.parameter.dataType;this.data = this.parameter.data;this.url  = this.getUrl();this.isAsync = (this.parameter.isAsync === undefined) ? "TRUE" : "FALSE"; //如果设置为FALSE,则会在返回结果前锁住浏览器this.success = (this.parameter.success === undefined) ? alert("请指定接收结果的函数") : this.parameter.success;this.xhr.onreadystatechange = function(){$$.getResponse();};this.xhr.open(this.type, this.url, this.isAsync);this.send();},getUrl : function(){if(this.type === "POST"){return this.parameter.url;}if(this.data === undefined){return this.parameter.url;}if(this.data.substr(0, 1) === "?"){return this.parameter.url + this.data;}else{return this.parameter.url + "?" + this.data;}},send : function(){if(this.type === "POST"){this.xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");this.xhr.send(this.data);}else{this.xhr.send();}},getResponse : function(){if(this.xhr.readyState === 4 && this.xhr.status === 200){if(this.parameter.dataType === "document"){this.parameter.success(this.xhr.responseXML);}else{this.parameter.success(this.xhr.responseText);}}}};

  

转载于:https://www.cnblogs.com/secern/p/3620066.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/34122.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息