Ajax的小封装 get,post请求
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-07-25 09:08:20
original
941 Les gens l'ont consulté
Ajax的小封装对ajax的get请求的小封装
-
- function Ajax() {
- var xhr =null;
- if(window.XMLHttpRequest) {
- xhr = new XMLHttpRequest();
- } else {
-
- xhr = new ActiveXObject("Microsoft.XMLHttp");
- }
- this.get=function(url,success,fail){ //get请求
- xhr.open("GET", "1.jsp",true);
- xhr.onreadystatechange=function(){
- if(xhr.readyState==4) {
- alert(xhr.status);
- if(xhr.status==200) {
- var txt = xhr.responseText;
- txt = eval("("+txt+")");
- var ch = txt.charAt(0);
- if(ch==" var xml = xhr.responseXML;
- success(eval("("+xml+")"));
- } else if(ch=="["||ch=="{") {//json类型
- txt = eval("("+txt+")");
- success(txt);
- } else {//不知道直接返回
-
- success(txt);
- }
- } else {
- if(fail) {
- fail(xhr.status);
- }
- }
- }
-
- };
- xhr.send(null);
-
- };
-
-
- this.post = function (url,param,success,fail) {//post请求
- xhr.open("POST", "1.jsp",true);
- xhr.onreadystatechange=function(){
- if(xhr.readyState==4) {
- alert(xhr.status);
- if(xhr.status==200) {
- var txt = xhr.responseText;
- var ch = txt.charAt(0);
- if(ch==" var xml = xhr.responseXML;
- success(eval("("+xml+")"));
- } else if(ch=="["||ch=="{") {//json类型
- txt = eval("("+txt+")");
- success(txt);
- } else {//不知道直接返回
-
- success(txt);
- }
-
- } else {
- if(fail) {
- fail(xhr.status);
- }
- }
- }
-
- };
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.send(param);
- };
-
- }
复制代码
|
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
-
2025-02-26 03:58:14
-
2025-02-26 03:38:10
-
2025-02-26 03:17:10
-
2025-02-26 02:49:09
-
2025-02-26 01:08:13
-
2025-02-26 00:46:10
-
2025-02-25 23:42:08
-
2025-02-25 22:50:13
-
2025-02-25 21:54:11
-
2025-02-25 20:45:11