简体   繁体   中英

Get data ajax from file local to server in pure javascript

I want to get data ajax from file local to server. I'm writting in Js as follow:

var UrlDetails = 'http://192.xxx.x.xxx:7000';
function createCORSRequest(method, url, asynch) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open(method, url, true);
        xhr.setRequestHeader('MEDIBOX', 'login');
        xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    } else if (typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open(method, url, asynch);
    } else {
        // CORS not supported.
        xhr = null;
    }
    return xhr;
}

function getDoLogin(e, callback) {
    var url = UrlDetails + '/api/loginGET?username=' + e.id + '&password=' + e.password + '&f=json';
    var xhr = createCORSRequest('GET', url, true);
    if (xhr) {
        xhr.onreadystatechange = function () {
            try {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        callback(JSON.parse(xhr.responseText));
                    } else {
                        xhr.status === 403 ? modalShow() : errorServer(xhr.status);
                    }
                }
            }
            catch (e) {
                alert('Caught Exception: ' + e.description);
            }
        };
        xhr.send();
    }
}

I call function getDoLogin(e, f); And I get a error from Chrome: XMLHttpRequest cannot load http://192.xxx.x.xxx:7000/api/loginGET?username=1&password=1&f=json . Response for preflight has invalid HTTP status code 405

It's only running with Jquery:

function getDoLoginJQuery(e) {
    return $.Deferred(function (d) {
        $.ajax({
            type: 'get',
            url: UrlDetails +'/loginGET?username=' + e.id + '&password=' + e.password + '&f=json',
            cache: 'false',
            dataType: 'json'
        }).done(function (a, b) {
            d.resolve(a)
        }).fail(function (a, b, c) {
            d.reject(a, b, c);
        })
    })
}

I don't know how to write in JS to run exactly. (I'm sorry. My English is not good.)

Try to remove setRequestHeader , like this:

function createCORSRequest(method, url, asynch) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open(method, url, true);
        // xhr.setRequestHeader('MEDIBOX', 'login');
        // xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    } else if (typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open(method, url, asynch);
    } else {
        // CORS not supported.
        xhr = null;
    }
    return xhr;
}

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM