var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://www.site.com/', true, 'username', 'password');
xhr.withCredentials = true;
xhr.timeout = 120000;
xhr.setRequestHeader('My-Header', '123');
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200 || xhr.status === 0) {
console.log('status: ' + xhr.status === 1223 ? 204 : xhr.status);
console.log('statusText: ' + xhr.status === 1223 ? 'No Content' : xhr.statusText);
console.log('getAllResponseHeaders: ' + xhr.getAllResponseHeaders());
console.log('getResponseHeader: ' + xhr.getResponseHeader('Content-Type'));
console.log('responseText: ' + xhr.responseText);
} else {
console.log('Error in request.');
}
}
};
xhr.onabort = function () {
if (!xhr) {return;}
xhr = null;
console.log('Aborted.');
};
xhr.ontimeout = function () {
xhr = null;
console.log('Timeout.');
};
xhr.onerror = function (error) {
xhr = null;
console.log('Error.');
console.log(error.message);
};
xhr.send(JSON.stringify({a: 1, b: 2}));
setTimeout(function () {
if (xhr) {xhr.abort();}
}, 1000);
Комментариев нет:
Отправить комментарий