ilinsky / xmlhttprequest Goto Github PK
View Code? Open in Web Editor NEWXMLHttpRequest.js - Standard-compliant cross-browser XMLHttpRequest object implementation
Home Page: http://www.ilinsky.com/articles/XMLHttpRequest/
License: Other
XMLHttpRequest.js - Standard-compliant cross-browser XMLHttpRequest object implementation
Home Page: http://www.ilinsky.com/articles/XMLHttpRequest/
License: Other
The XMLHttpRequest level 2 specification recently added timeout support, AFAIK by "inheriting" the the feature introduced in IE8. Nightly Gecko builds already support this feature, which will likely be shipped in Firefox 13, so it would be great to have this feature available in a cross-browser way:
timeout
attribute and/or ontimeout
event and use the native feature, when available;In order to work around and IE memory leak issue, the following code tries to detect whether the browser is IE:
var bIE = window.document.all && !window.opera;
window.document.all exists in various versions of Firefox and therefore isn't a good enough way to determine whether the browser is IE. Later the bIE variable is checked and an event handler is registered onunload:
window.attachEvent("onunload", fOnUnload);
which of course fails for Firefox that lacks attachEvent (and doesn't need ant memory leak workaround).
I'm trying to use xmlhttprequest.js to intercept an ajax request and request a different url instead. The code all looks correct, but it's failing silently with no hints in the developer console. Here is a live preview along with the code:
http://embed.plnkr.co/5Vpdohf69anQlUutHl7Y/preview
Am I doing something wrong, or is this just not possible?
Thanks in advance for any tips.
Using xmlHtjp = new window.XMLHttpRequest()
...
if (xmlHttp.readyState === XMLHttpRequest.DONE) {
if (xmlHttp.status === 200) {
xml = xmlHttp.responseXML.documentElement;
is failing on IE6, 7, 8, 9 [all the versions I have current access to] . Traced to fGetDocument. The check for errors in the document needs to be something like:
if ((bIE && oDocument.parseError && oDocument.parseError.errorCode !== 0) || ...
instead of
if ((bIE && oDocument.parseError !== 0) || ...
Has no-one been using responseXML with IE? I checked back to the earliest version on github, and it has the same incorrect parseError test. I tested my fix against IE6, 7, 8, 9. With that bIE in the check, nothing else should get affected.
The line 474 in the code is wrong, as below:
// Fake event
oRequest.dispatchEvent({
'type': "readystatechange",
'bubbles': false,
'cancelable': false,
'timeStamp': new Date + 0
});
new Date + 0 will results a string with something like "Tue Jun 11 2013 13:07:09 GMT+0800 (HKT)0", by the specification, it should be a timestamp in long type. So it should be:
// Fake event
oRequest.dispatchEvent({
'type': "readystatechange",
'bubbles': false,
'cancelable': false,
'timeStamp': new Date().getTime()
});
I have trouble with loading big datasets (28mb json, zipped 645kb) in chrome with OpenLayers. I debugged the code and the problem appeared to be in xmlhttprequest.
Function: "cXMLHttpRequest.prototype.open"
Inner function: "this._object.onreadystatechange = function() {"
This function is accessed alot of times in chrome while handling the request. The total request takes long because the call:
"fSynchronizeValues(oRequest);" seems to be expensive.
I replaced the first call to "fSynchronizeValues" with the following code:
if (oRequest._object.readyState===4 && oRequest._object.status === 200) {
fSynchronizeValues(oRequest);
}
The total request is much faster now. From 2 minutes to just a few seconds....
Im not sure though if this is the correct solution
Regards Mark
I'm loading test JSON data from a text file on a local web server (using IE) and thus don't need to send anything. Normally I send undefined or "", and it works using the native xmlHttpRequest object, but not with your wrapper.
According to the specification I found (http://xhr.spec.whatwg.org/#the-send()-method): "If data is null, do not include a request entity body and go to the next step." However, the data is not null when it's undefined or empty, and thus most browsers I've tested will send an empty request in these cases, AND return the response (or file in this case).
Hi,
i wish to use this library in another project. The project is located at:
https://github.com/gimmi/jsonrpcjs
and it's licensed as Apache. I can change the license to LGPL without problem.
Another thing that i wish to do is that my intentions is to use xmlhttprequest internally, but the unmodified version of the code replace XmlHttpRequest in the global scope.
Can you update the code so that by default XmlHttpRequest is untouched?
Can i include your sources in jsonrpcjs lib?
Thanks!
The XMLHttpRequestUpload object that is accessible through the upload property is not accessible through the new XHR object. This property, along with the other missing ones, should be surfaced so it doesn't break existing functionality of the original XHR.
when using ie 10.
responseXML value returns null,
So I added code as shown below
" try { this._object.responseType = "msxml-document"; } catch(err){}"
before request open.
according to
http://blogs.msdn.com/b/ie/archive/2012/07/19/xmlhttprequest-responsexml-in-ie10-release-preview.aspx
The following function has a bug in it:
function fGetDocument(oRequest) {
var oDocument = oRequest.responseXML;
var sResponse = oRequest.responseText;
// Try parsing responseText
if (bIE && sResponse && oDocument && !oDocument.documentElement && oRequest.getResponseHeader("Content-Type").match(/[^\/]+\/[^\+]+\+xml/)) {
oDocument = new window.ActiveXObject("Microsoft.XMLDOM");
oDocument.async = false;
oDocument.validateOnParse = false;
oDocument.loadXML(sResponse);
}
// Check if there is no error in document
if (oDocument){
if ((bIE && oDocument.parseError != 0) || !oDocument.documentElement || (oDocument.documentElement && oDocument.documentElement.tagName == "parsererror")) {
return null;
}
}
return oDocument;
}
Instead of
if ((bIE && oDocument.parseError != 0) || !oDocument.documentElement || (oDocument.documentElement && oDocument.documentElement.tagName == "parsererror")) {'''
it should be
if (if ((bIE && oDocument.parseError.errorCode != 0)'''
Where it should be checking the parseError.errorCode property to see if there is an error NOT the object.
1.Dojo components get affect by including this scripts. did anyone face this issues ?
2.if we include the script two times by mistake, open ( XMLHttpRequest.onopen ) method get invokes two times.
The changes introduced in commit #8f1e7b940a476b0964483392a11005dc313dd3ff lead to an infinite loop between lines 33 and 27 by simply executing:
new XMLHttpRequest();
I suppose that this code will actually work if XHR object was first replaced by this library and after that replaced by yet another library but the way it is right now, the code simply doesn't work if this library is used stand-alone. Simply reverting the changes introduced by the above commit fixes the problems, but supposedly negates the effect intended by the original committer. Furthermore, the author forgot to replace the check in line 38
if (bGecko && oXMLHttpRequest.wrapped) {
cXMLHttpRequest.wrapped = window.XMLHttpRequest.wrapped;
}
because oXMLHttpRequest
variable doesn't exist any more after the commit.
I was wondering if it would make sense to update the send
method to check for the HTTP method.
In case the requested method is not supported by the browser we could add the X-HTTP-Method-Override
header which seems to be common these days.
Would you be open for such a modification or should it perhaps be added as syntactic sugar to a wrapper object...?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.