xmlhttprequest-level2

Reusing XMTHttpRequest object?

痞子三分冷 提交于 2021-02-19 05:53:10
问题 I am trying to rewrite some ajax code to make it fit better with the needs for autocomplete. In that situation you have to abort a previous request sometimes, xhr.abort() , so it feels rather natural to reuse that XMLHttpRequest object (which I just called xhr here). I am trying to understand whether it is a good or bad idea to reuse the XMLHttpRequest object. What pros and cons can you see? PS: This rewrite will use native ES6-style Promise so it can only run in newer web browsers. 回答1: Just

JS ProgressEvent is only fired when finished

纵然是瞬间 提交于 2019-12-21 05:05:24
问题 I am having some problems getting my upload progress bar to work properly. According to the XMLHttpRequest Level 2 specs, I attached event listeners for loadstart and progress like this: var xhr = $.ajaxSettings.xhr(); xhr.upload.addEventListener('loadstart', function(e) {progressCallback(0);}); xhr.upload.addEventListener('progress', function (e) { progressCallback(e.loaded / e.total); }); $.ajax({ url: url, type: 'POST', processData: false, contentType: false, data: formData, xhr: function

Uploading a file with XMLHttprequest - Missing boundary in multipart/form-data

假装没事ソ 提交于 2019-12-18 04:49:09
问题 I'm uploading a file with XMLHttprequest. Here is the JS function, that uploads a file: var upload = function(file) { // Create form data var formData = new FormData(); formData.append('file', file); var xhr = new XMLHttpRequest(); // Open xhr.open('POST', this.options.action); // Set headers xhr.setRequestHeader("Cache-Control", "no-cache"); xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); xhr.setRequestHeader("Content-Type", "multipart/form-data"); xhr.setRequestHeader("X-File

Chrome extension: how to pass ArrayBuffer or Blob from content script to the background without losing its type?

断了今生、忘了曾经 提交于 2019-12-17 10:29:24
问题 I have this content script that downloads some binary data using XHR, which is sent later to the background script: var self = this; var xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.responseType = 'arraybuffer'; xhr.onload = function(e) { if (this.status == 200) { self.data = { data: xhr.response, contentType: xhr.getResponseHeader('Content-Type') }; } }; xhr.send(); ... later ... sendResponse({data: self.data}); After receiving this data in background script, I'd like to form

Deadly CORS when http://localhost is the origin

给你一囗甜甜゛ 提交于 2019-12-16 19:40:47
问题 I am stuck with this CORS problem, even though I set the server (nginx/node.js) with the appropriate headers. I can see in Chrome Network pane -> Response Headers: Access-Control-Allow-Origin:http://localhost which should do the trick. Here's the code that I now use to test: var xhr = new XMLHttpRequest(); xhr.onload = function() { console.log('xhr loaded'); }; xhr.open('GET', 'http://stackoverflow.com/'); xhr.send(); I get XMLHttpRequest cannot load http://stackoverflow.com/. Origin http:/

Deadly CORS when http://localhost is the origin

放肆的年华 提交于 2019-12-16 19:40:32
问题 I am stuck with this CORS problem, even though I set the server (nginx/node.js) with the appropriate headers. I can see in Chrome Network pane -> Response Headers: Access-Control-Allow-Origin:http://localhost which should do the trick. Here's the code that I now use to test: var xhr = new XMLHttpRequest(); xhr.onload = function() { console.log('xhr loaded'); }; xhr.open('GET', 'http://stackoverflow.com/'); xhr.send(); I get XMLHttpRequest cannot load http://stackoverflow.com/. Origin http:/

In CORS, Are POST request with credentials pre-flighted ?

半世苍凉 提交于 2019-12-13 15:24:09
问题 In MDN Access Cotrol doc, GET request with credentials are not preflighted. But if response headers doesn't include Access-Control-Allow-Credentials: true then response will not be available to the invoking client. If this behaviour same for POST (Simple POST request with credentials - Content Type may be form-data) request as well, there is risk that POST might change the server state though response may not be made available to client. Is this assumption correct? OR POST request with

xhr.upload.onprogress doesn't work

混江龙づ霸主 提交于 2019-12-12 08:34:49
问题 Everything in the following code will work, except it will never fire the xhr.upload.onprogress event. $(function(){ var xhr; $("#submit").click(function(){ var formData = new FormData(); formData.append("myFile", document.getElementById("myFileField").files[0]); xhr = new XMLHttpRequest(); xhr.open("POST", "./test.php", true); xhr.send(formData); xhr.onreadystatechange = function(){ if(xhr.readyState === 4 && xhr.status === 200){ console.log(xhr.responseText); } } xhr.upload.onprogress =

How to know which <input> is submited when several ones are defined within a <form>?

雨燕双飞 提交于 2019-12-11 07:26:49
问题 I want to implement the below simple JavaScript function submitForm() based on XMLHttpRequest and FormData . This functions works well on the first <form> but fails on the second one: the function should use input.formaction instead of form.action . How to detect the pressed <input> and retrieve the corresponding formaction ? ( Most of SO answers advise to use a framework (as jquery) for such processing. But I think learning solely pure JavaScript is easier than learning also JS frameworks.

How to wait for all XHR2 send calls to finish

五迷三道 提交于 2019-12-11 07:25:08
问题 I have done some code to upload multiple files from browser to server, while showing progressbars as well. XHR2 send calls are asynchronous. The problem is that I want to call some functions after all the XHR2 send calls are finished. Here is a very simplified snippet of my code: <input id="upload" multiple="multiple" /> <script> var files = document.getElementById("upload").files; for (var i = 0, length = files.length; i < length; i++) { var file = files[i]; var uploadFunc = function (arg) {