mrcio-ui
Version:
137 lines (120 loc) • 5.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports._Ajax = undefined;
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _cache = require("./cache");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Cache 原生Ajax 封装
* @Author gufuyan
* @Date 2018-08-30
*/
var AjaxFn =
//函数参数默认值
function AjaxFn() {
_classCallCheck(this, AjaxFn);
this.initServiceGet = function (url, async, token) {
var xmlhttp = void 0;
// if (window.XMLHttpRequest) {
// xmlhttp = new XMLHttpRequest();
// } else {
// xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// }
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
alert("浏览器不支持使用XMLHttpRequest,请使用谷歌浏览器或者360浏览器重试。重试不行请联系IT部。");
return false;
}
xmlhttp.onreadystatechange = function () {
var nullData = { "userId": null, "userImage": "", "sex": "F", "positionName": "", "userName": "" };
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// console.log("--------请求登录人信息---------gfy_test");
// console.log(xmlhttp.responseText);
var permission = eval('(' + xmlhttp.responseText + ')'),
loginUser = permission.errorCode == 0 ? permission.data : nullData;
_cache._Cache.setLocal("permission", JSON.stringify(loginUser));
} else {
_cache._Cache.setLocal("permission", JSON.stringify(nullData));
}
};
xmlhttp.open("GET", url, async);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//xmlhttp.setRequestHeader("Authorization","Bearer "+token);
xmlhttp.setRequestHeader("Authorization", token);
xmlhttp.send();
};
this.test = function () {
//创建对象
var xmlhttp, xmlDoc, txt, x;
// if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
// xmlhttp = new XMLHttpRequest();
// } else {// code for IE6, IE5
// xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// }
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
alert("浏览器不支持使用XMLHttpRequest,请使用谷歌浏览器或者360浏览器重试。重试不行请联系IT部。");
}
/*
open(method,url,async) 规定请求的类型、URL 以及是否异步处理请求。
method:请求的类型,GET 或 POST;url:文件在服务器上的位置;async:true(异步)或 false(同步)
send(string)将请求发送到服务器。string:仅用于 POST 请求
*/
//与 POST 相比,GET 更简单也更快
/*
在以下情况中,请使用 POST 请求:
无法使用缓存文件(更新服务器上的文件或数据库)
向服务器发送大量数据(POST 没有数据量限制)
发送包含未知字符的用户输入时,POST 比 GET 更稳定也更可靠
*/
//xmlhttp.open("GET","demo_get.asp?t=" + Math.random(),true);
//xmlhttp.open("GET","demo_get2.asp?fname=Bill&lname=Gates",true);
xmlhttp.open("GET", "test1.txt", true);
xmlhttp.send();
//如果需要像 HTML 表单那样 POST 数据,请使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定您希望发送的数据:
xmlhttp.open("POST", "ajax_test.asp", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("fname=Bill&lname=Gates");
/*
setRequestHeader(header,value);向请求添加 HTTP 头。header: 规定头的名称;value: 规定头的值
*/
//获得字符串形式的响应数据。
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
};
//获得 XML 形式的响应数据。
/*onreadystatechange 存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。
readyState 存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。
0: 请求未初始化 1: 服务器连接已建立 2: 请求已接收 3: 请求处理中 4: 请求已完成,且响应已就绪
status 200: "OK" 404: 未找到页面*/
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
xmlDoc = xmlhttp.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("title");
for (var i = 0; i < x.length; i++) {
txt = txt + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myDiv").innerHTML = txt;
}
};
};
}
/**
* 项目初始化服务
* @param url
* @param async
* @param token
*/
;
var _Ajax = new AjaxFn();
exports._Ajax = _Ajax;
//# sourceMappingURL=ajax.js.map