xmlhttprequest

XMLHttpRequest and Chrome developer tools don't say the same thing

让人想犯罪 __ 提交于 2019-12-21 04:51:38
问题 I'm downloading a ~50MB file in 5 MB chunks using XMLHttpRequest and the Range header. Things work great, except for detecting when I've downloaded the last chunk. Here's a screenshot of the request and response for the first chunk. Notice the Content-Length is 1024 * 1024 * 5 (5 MB). Also notice that the server responds correctly with the first 5 MB, and in the Content-Range header, properly specifies the size of the entire file (after the / ): When I copy the response body into a text

try-catch doesn't work with XMLHTTPRequest

微笑、不失礼 提交于 2019-12-21 03:22:07
问题 I am trying to use the try-catch statements to handle the errors from XMLHTTPRequest , like below: var xhr = new XMLHttpRequest(); xhr.open('POST', someurl, true); try{ xhr.sendMultipart(object); } catch(err){ error_handle_function(); } When there was a 401 error thrown by xhr.sendMultipart , the error_handle_function was not called. Any idea how to fix this? Thanks! 回答1: I think you can't catch server errors that way. you should be checking the status code instead: var xhr = new

try-catch doesn't work with XMLHTTPRequest

眉间皱痕 提交于 2019-12-21 03:21:39
问题 I am trying to use the try-catch statements to handle the errors from XMLHTTPRequest , like below: var xhr = new XMLHttpRequest(); xhr.open('POST', someurl, true); try{ xhr.sendMultipart(object); } catch(err){ error_handle_function(); } When there was a 401 error thrown by xhr.sendMultipart , the error_handle_function was not called. Any idea how to fix this? Thanks! 回答1: I think you can't catch server errors that way. you should be checking the status code instead: var xhr = new

Uploading files using AJAX without FormData (IE9)

大城市里の小女人 提交于 2019-12-21 01:19:38
问题 In IE9, FormData is not supported, which makes uploading files using XMLHttpRequest a lot less trivial. Can this be done? I've seen iFrames mentioned, and while I'm not opposed to writing some hairy code, I'm at a loss as to how to achieve this (there are many resources talking about uploading to an iFrame but not about how to get the file from the iFrame to the server). Using vanilla JavaScript (no third party libraries), how would one upload a file asynchronously without the use of FormData

How to deal with browser's limit of parallel requests per domain in case of a priority AJAX request?

别来无恙 提交于 2019-12-21 01:17:21
问题 Imagine as given the following situation: There are about 20 requests (or even more) which has been triggered by our website. These can be any kind of requests - we don't know how to trigger them again. On this website, all the requests target the same url. The requests can have subscribed event listeners. In case of using Chrome, the first 6 requests are sent and the others are waiting in a queue to be sent (because of the parallel request limit per domain). At this moment the webpage

Firefox & AJAX Junk after document element

偶尔善良 提交于 2019-12-20 18:07:20
问题 Im using a page fetch script to dynamically load a web page into a div. Heres the code. BTW Im using Firefox w/ Kubuntu function fetch(URL, divId) { req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0"); req.open("GET", URL); req.onreadystatechange = function() { if (req.readyState == 4 && req.status == 200) { document.getElementById(divId).innerHTML = req.responseText; } } req.send(null); } When i try to get it to load a page nothing happens and I get

Firefox & AJAX Junk after document element

假装没事ソ 提交于 2019-12-20 18:06:33
问题 Im using a page fetch script to dynamically load a web page into a div. Heres the code. BTW Im using Firefox w/ Kubuntu function fetch(URL, divId) { req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0"); req.open("GET", URL); req.onreadystatechange = function() { if (req.readyState == 4 && req.status == 200) { document.getElementById(divId).innerHTML = req.responseText; } } req.send(null); } When i try to get it to load a page nothing happens and I get

XMLHttpRequest的POST同步请求代码

拈花ヽ惹草 提交于 2019-12-20 17:45:09
原生JS的异步POST请求回调代码如下: function test(url, param, callback) { var xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-Type", "application/json;charset=utf-8"); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // 异步成功回调 var result = xmlhttp.responseText; if (callback) { callback(result); } } }; xmlhttp.send(JSON.stringify(param)); } 现在要改成同步POST返回参数,关键点虽然在于xmlhttp.open参数改成false(网络上到处都是),但是具体怎么返回参数查了好多才发现方法: function test(url, param) { var xmlhttp = new XMLHttpRequest(); var result = null xmlhttp.open(

XMLHttpRequest throwing InvalidSateError saying “Object state must be opened”

你离开我真会死。 提交于 2019-12-20 17:34:07
问题 The code - "use strict"; var AJAX = function (params) { this.server ={}; this.url = params.url; this.method = params.method; this.dataType = params.dataType; this.formData = params.formData; this.init = function(){ if(typeof XMLHttpRequest != 'undefined'){ this.server = new XMLHttpRequest(); this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); this.server.setRequestHeader('Content-length', this.formData.length); this.server.setRequestHeader('Connection', 'close')

A JavaScript frontend logging system that logs to our backend?

被刻印的时光 ゝ 提交于 2019-12-20 12:34:33
问题 We have an established logging system for our server-side services. Specifically, our Django project makes heavy use of the Python logging module, so call calls to logger.info() , logger.warn() and logger.error() get picked up by our centralized logging system. I would like an equivalent on our frontend, and I've got a few ideas: There would be some sort of custom logging object exposed via JavaScript that would send messages to backend via an XmlHttpRequest. I'd like to have equivalent