Bug Tracker

Opened 9 years ago

Closed 8 years ago

Last modified 7 years ago

#5866 closed bug (fixed)

.ajax() fails in IE7 when using an absolute url without protocol

Reported by: skrings Owned by: jaubourg
Priority: low Milestone: 1.next
Component: ajax Version: 1.5rc1
Keywords: needsreview, ajaxrewrite Cc:
Blocked by: Blocking:

Description

From jQuery 1.3.2 to 1.4 the detection of the XHR object has changed. In 1.3.2 every IE uses the ActiveXObject if available. In 1.4 every IE uses the ActiveXObject only if the location protocol is 'file:'.

This causes a problem in IE7, because in IE7 the implementation of XMLHttpRequest is a little bit buggy and in IE8 it seems ok. When you open a new XHR in IE7 with an absolute URL with a protocol and use the XMLHttpRequest object it fails with an error: 'Access is denied'.

IE7 with XMLHttpRequest: fails | IE7 with ActiveXObject : ok

IE8 with XMLHttpRequest: ok | IE8 with ActiveXObject : ok

Take a look at the attached test case, just upload it to any webserver.

Suggested fix #1:

Change the XHR handling so every IE uses the ActiveXObject if available.

Suggested fix #2:

Look for absolute URLs bevore opening the XHR and add a missing protocol.

Suggested fix #3:

Extend the XHR handling so that every IE uses the ActiveXObject if available and the protocol is missing or 'file:'.

Possibile solution:

I would prefer fix #3, so here is my fix which is also inlcuded in the test case. The file is named jquery-1.4.fix.js, just compare it with the 1.4 tag. One problem i have is that i cannot differentiate IE7 and IE8 by the used object. So if the protocol is missing or 'file:' the IE8 will use the ActiveXObject also.

Best regards,

Sören

Some code:

Extended XHR handling

xhr: window.XMLHttpRequest && !window.ActiveXObject ?

function() {

return new window.XMLHttpRequest();

} : function(protocol) {

if (window.XMLHttpRequest && protocol !== "file:" && protocol !== true) {

return new window.XMLHttpRequest();

} else {

try {

return new window.ActiveXObject("Microsoft.XMLHTTP");

} catch(e) {};

}

},

Create the request object

var xhr = s.xhr(protocol);

Attachments (2)

ie7test.zip (89.4 KB) - added by skrings 9 years ago.
Test case and possible fix included.
ie7test-1.4.3.zip (91.2 KB) - added by skrings 9 years ago.
Test case 1.4.3.-pre with fix

Download all attachments as: .zip

Change History (11)

Changed 9 years ago by skrings

Attachment: ie7test.zip added

Test case and possible fix included.

comment:1 Changed 9 years ago by dmethvin

So do you mean that when it fails the url is something like "http://something.com/test.htm" and the requesting page is at something.com? An example would help.

comment:2 Changed 9 years ago by skrings

No i mean an url like

//something.com/test.htm

so the request handler (XMLHttpRequest or ActiveXObject) should add the current protocol like 'http:' or 'https:'.

Please take a look at the test case, i log the used url to the console.

The interest is the changed XHR handling from 1.3.2 to 1.4. At this point the api documentation is out of date. http://api.jquery.com/jQuery.ajax/ "Defaults to the ActiveXObject when available (IE)" In 1.4 the ActiveXObject is used only if the protocol is 'file:'.

Last edited 8 years ago by skrings (previous) (diff)

comment:3 Changed 9 years ago by skrings

Simple fix by ajaxSetup:

// Use the ActiveXObject everytime when it is available
$.ajaxSetup({           
                xhr: !!window.ActiveXObject 
                                ? function(){
                                        try {
                                                return new window.ActiveXObject("Microsoft.XMLHTTP");
                                        } catch(e){}
                                } 
                                : function(){ 
                                        return new XMLHttpRequest();
                                }
        });
Last edited 8 years ago by skrings (previous) (diff)

Changed 9 years ago by skrings

Attachment: ie7test-1.4.3.zip added

Test case 1.4.3.-pre with fix

comment:4 Changed 9 years ago by skrings

I have attched an updated test case and landed a fix for the problem. It would be glad if someone could review my commits.

See my version here:

http://github.com/skrings/jquery/blob/master/src/ajax.js

Edit: Removed git links.

Last edited 8 years ago by skrings (previous) (diff)

comment:5 Changed 8 years ago by addyosmani

Keywords: needsreview added
Priority: majorlow

comment:6 Changed 8 years ago by Rick Waldron

Keywords: ajaxrewrite added

comment:7 Changed 8 years ago by skrings

jsFiddle for easier review: http://jsfiddle.net/WuGZh/

comment:8 Changed 8 years ago by jitter

Milestone: 1.4.11.next
Owner: set to jaubourg
Status: newassigned
Version: 1.41.5rc1

3rd party pull request

comment:9 Changed 8 years ago by jaubourg

Resolution: fixed
Status: assignedclosed

Fixes #5866. Issue number in previous commit was wrong both in comments and commit message. See https://github.com/jquery/jquery/commit/0e5b341cc0f3f9bf0f6659e09704f2267cfdfdba for previous commit.

Changeset: d7d64713a72c67243b279b9dcb16ae9fbb825c17

Note: See TracTickets for help on using tickets.