ajax IE8、IE9的跨域兼容

    ajax (url, params, successCallback, errorCallback) {
     
        var xdr;
        try {
     
            xdr = new XDomainRequest();
        } catch (e) {
     
            console.log('ie8,9跨域对象创建失败'+e);
        }
        if (xdr) {
     
            xdr.onerror = function () {
     
                errorCallback && errorCallback();
            };
            xdr.ontimeout = function () {
     
                errorCallback && errorCallback();
            };
            xdr.onload = function () {
     
                successCallback && successCallback(JSON.parse(xdr.responseText));
            };
            xdr.timeout = 10000;
            xdr.open('POST',this.state.dzyyzzApiHead + url);
            xdr.send(JSON.stringify(params));
        } else {
     
            $.ajax({
     
                url: this.state.dzyyzzApiHead + url,
                data: JSON.stringify(params),
                async: false,
                contentType: 'application/json;charset=utf-8',
                type: 'POST',
                success: function (res) {
     
                    successCallback && successCallback(res);
                },
                error: function () {
     
                    errorCallback && errorCallback();
                }
            });
        }
    }

https://www.jianshu.com/p/4f04013b8b91

你可能感兴趣的:(ajax IE8、IE9的跨域兼容)