Saturday 13 October 2018

Native XMLHttpRequest

var newName = 'John Smith',
    xhr = new XMLHttpRequest();

xhr.open('POST', 'myservice/username?id=some-unique-id');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
    if (xhr.status === 200 && xhr.responseText !== newName) {
        alert('Something went wrong.  Name is now ' + xhr.responseText);
    }
    else if (xhr.status !== 200) {
        alert('Request failed.  Returned status of ' + xhr.status);
    }
};
xhr.send(encodeURI('name=' + newName));

Credit: https://blog.garstasio.com/you-dont-need-jquery/ajax/#posting
Test:https://www.measurethat.net/Benchmarks/ShowResult/13128

No comments:

Post a Comment