let origin = 'http://www.baidu.com' // Domain name of the back-end interface
const address = {
    // info: Origin + '/template/getinfo', // backend interface
}

export function httpRequest(obj, successfun, errFun){
    return new Promise((resolve, reject) = > {
        var xmlHttp = null;
        Older versions of Internet Explorer (IE5 and IE6) use the ActiveX object: XMLHTTP =new ActiveXObject(" microsoft.xmlhttp ")
        if(window.XMLHttpRequest){
            //code for all new browsers()
            xmlHttp = new XMLHttpRequest;
        }else if(window.ActiveXObject){
            //code for IE5 and IE6
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        // Determine whether the request is supported
        if(xmlHttp == null){
            alert("Browser does not support xmlHttp");
            return;
        }
        // Request mode, converted to uppercase
        var httpMethod = (obj.method || "Get").toUpperCase();
        // Data type
        var httpDataType = obj.dataType || 'json';
        //url
        var httpUrl = obj.url;
        // Asynchronous request
        var async = true;
        // Parameters are processed when a POST request is made
        if(httpMethod == "POST") {// Parameters in the request body Post Request parameters are in the following format: param1=test&param2=test2
            var data = obj.data || {};
            console.log(obj.data)
            var requestData = ' ';
            for(var key in data){
                requestData = requestData + key + "=" + data[key] + "&";
            }
            if(requestData == ' '){
                requestData = ' ';
            }else{
                requestData = requestData.substring(0, requestData.length - 1); }}// Request the interface
        if(httpMethod == 'GET'){
            xmlHttp.open("GET", httpUrl, async);
            xmlHttp.send(null);
        }else if(httpMethod == "POST"){
            xmlHttp.open("POST", httpUrl, async);
            xmlHttp.setRequestHeader("Content-Type"."application/x-www-form-urlencoded");
            xmlHttp.send(requestData);
        }
        
        // onreadyStatechange is an event handle. Its value (state_Change) is the name of a function that is triggered when the state of the XMLHttpRequest object changes. The state changes from 0 (uninitialized) to 4 (complete). Only when the state is 4 do we execute the code
        xmlHttp.onreadystatechange = function(){
            //complete
            if(xmlHttp.readyState == 4) {// We use promise instead of a callback after a successful request
                if(xmlHttp.status == 200) {let res = JSON.parse(xmlHttp.responseText);
                    if (res && res.errno == 0) {
                        resolve(res);
                    } else {
                        reject(res);
                    }
                    // Request a callback that successfully executed
                    // successfun(JSON.parse(xmlHttp.responseText));
                } else {
                	// Request failed callback functionerrFun; }}}})}Copy the code