var xhr = new XMLHttpRequest(); xhr.open('GET', '/path/to/image.png', true); // , xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = function(e) { if (this.readyState == 4 && this.status == 200) { var binStr = this.responseText; for (var i = 0, len = binStr.length; i < len; ++i) { var c = binStr.charCodeAt(i); //String.fromCharCode(c & 0xff); var byte = c & 0xff; } } }; xhr.send();
BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder; var xhr = new XMLHttpRequest(); xhr.open('GET', '/path/to/image.png', true); xhr.responseType = 'arraybuffer'; xhr.onload = function(e) { if (this.status == 200) { var bb = new BlobBuilder(); bb.append(this.response); // : xhr.responseText var blob = bb.getBlob('image/png'); /*...*/ } }; xhr.send();
var xhr = new XMLHttpRequest(); xhr.open('GET', '/path/to/image.png', true); xhr.responseType = 'arraybuffer'; xhr.onload = function(e) { var uInt8Array = new Uint8Array(this.response); // this.response == uInt8Array.buffer // var byte3 = uInt8Array[4]; // 4- /*...*/ }; xhr.send();
xhr.responseType='blob'
(Now available only in Chrome crbug.com/52486 ): window.URL = window.URL || window.webkitURL; var xhr = new XMLHttpRequest(); xhr.open('GET', '/path/to/image.png', true); xhr.responseType = 'blob'; xhr.onload = function(e) { if (this.status == 200) { var blob = this.response; var img = document.createElement('img'); img.onload = function(e) { window.URL.revokeObjectURL(img.src); // Clean up after yourself. }; img.src = window.URL.createObjectURL(blob); document.body.appendChild(img); /*...*/ } }; xhr.send();
function sendText(txt) { var xhr = new XMLHttpRequest(); xhr.open('POST', '/server', true); xhr.onload = function(e) { if (this.status == 200) { console.log(this.responseText); } }; xhr.send(txt); } sendText('test string');
function sendTextNew(txt) { var xhr = new XMLHttpRequest(); xhr.open('POST', '/server', true); xhr.responseType = 'text'; // <<< xhr.onload = function(e) { if (this.status == 200) { console.log(this.response); // <<< } }; xhr.send(txt); } sendText2('test string');
function sendForm() { var formData = new FormData(); formData.append('username', 'johndoe'); // <<< formData.append('id', 123456); // <<< var xhr = new XMLHttpRequest(); xhr.open('POST', '/server', true); xhr.onload = function(e) { /*...*/ }; xhr.send(formData); // <<<
<form id="myform" name="myform" action="/server"> <input type="text" name="username" value="johndoe"> <input type="number" name="id" value="123456"> <input type="submit" onclick="return sendForm(this.form);"> </form>
function sendForm(form) { var formData = new FormData(form); // FormData HTMLFormElement formData.append('secret_token', '1234567890'); // var xhr = new XMLHttpRequest(); xhr.open('POST', form.action, true); xhr.onload = function(e) { /*...*/ }; xhr.send(formData); return false; // }
<input type="file">
) - FormData can work with them. Just add the file (s) and the browser will execute a multipart/form-data
request when the send()
method is called. It is very convenient! function uploadFiles(url, files) { var formData = new FormData(); for (var i = 0, file; file = files[i]; ++i) { formData.append(file.name, file); } var xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.onload = function(e) { /*...*/ }; xhr.send(formData); // multipart/form-data } document.querySelector('input[type="file"]').addEventListener('change', function(e) { uploadFiles('/server', this.files); }, false);
<progress min="0" max="100" value="0">0% complete</progress>
function upload(blobOrFile) { var xhr = new XMLHttpRequest(); xhr.open('POST', '/server', true); xhr.onload = function(e) { /*...*/ }; // var progressBar = document.querySelector('progress'); xhr.upload.onprogress = function(e) { // <<< if (e.lengthComputable) { progressBar.value = (e.loaded / e.total) * 100; progressBar.textContent = progressBar.value; // progress } }; xhr.send(blobOrFile); // <<< } var BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder; var bb = new BlobBuilder(); bb.append('hello world'); // <<< upload(bb.getBlob('text/plain')); // <<<
function sendArrayBuffer() { var xhr = new XMLHttpRequest(); xhr.open('POST', '/server', true); xhr.onload = function(e) { /*...*/ }; var uInt8Array = new Uint8Array([1, 2, 3]); // <<< xhr.send(uInt8Array.buffer); // <<<< }
example.com
and we need to get data from www.example2.com
www.example2.com
. Usually, if you try to make such an AJAX request, the request will not be executed and the browser will throw the exception “origin mismatch”. With CORS www.example2.com
www.example2.com
may decide to allow our application to execute the request from example.com
or not by adding just one header: Access-Control-Allow-Origin: http://example.com
Access-Control-Allow-Origin
header can be issued to one site or to any site from any domain: Access-Control-Allow-Origin: *
Access-Control-Allow-Origin
header:example.com
server to the server www.example2.com
www.example2.com
: var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.example2.com/hello.json'); xhr.onload = function(e) { var data = JSON.parse(this.response); /*...*/ } xhr.send();
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; function onError(e) { console.log('Error', e); } var xhr = new XMLHttpRequest(); xhr.open('GET', '/path/to/image.png', true); xhr.responseType = 'arraybuffer'; // <<< // xhr.onload = function(e) { // <<< // window.requestFileSystem(TEMPORARY, 1024 * 1024, function(fs) { // <<< // - fs.root.getFile('image.png', {create: true}, function(fileEntry) { // fileEntry.createWriter(function(writer) { writer.onwrite = function(e) { /*...*/ }; writer.onerror = function(e) { /*...*/ }; // Blob var bb = new BlobBuilder(); // <<< bb.append(this.response); // <<< // writer.write(bb.getBlob('image/png')); // <<< }, onError); }, onError); }, onError); }; xhr.send();
window.BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder; function upload(blobOrFile) { var xhr = new XMLHttpRequest(); xhr.open('POST', '/server', true); xhr.onload = function(e) { /*...*/ }; xhr.send(blobOrFile); } document.querySelector('input[type="file"]').addEventListener('change', function(e) { var blob = this.files[0]; const BYTES_PER_CHUNK = 1024 * 1024; // 1MB const SIZE = blob.size; var start = 0; var end = BYTES_PER_CHUNK; while(start < SIZE) { // : blob.slice . http://goo.gl/U9mE5 if ('mozSlice' in blob) { var chunk = blob.mozSlice(start, end); } else { var chunk = blob.webkitSlice(start, end); } upload(chunk); start = end; end = start + BYTES_PER_CHUNK; } }, false); })();
Source: https://habr.com/ru/post/120917/
All Articles