views:

134

answers:

3

I try to do an AJAX call with jQuery and $.post in Internet Explorer, but all I get is an error saying "Permission denied". The problem is kinda weird since it occurs only when I access a page after I was on any other page.

For instance I type the URL in the adress line and let IE load the page. Then I click on a button so the script should start loading JSON data. (The script providing the data lies on the same server and I access it with a relative URL, so using a different domain is not the problem here. Even tried to use a absolute URL with the same host part.)

But when I refresh the page then and try it again it works! Same thing when I come to that page from another page. At first nothing works, but when I click "refresh" everything is fine.

IE gives me the error message "Permission denied" while in every other browser I don't notice this behaviour. Since I have tried many things and still cannot imagine where the problem lies I'd like to ask you what you think the problem might be?

edit: A small example:

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de"> 
    <head> 
        <script type="text/javascript" src="/ietest/jquery.js"></script> 
        <script type="text/javascript" src="/ietest/test.js"></script> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    </head> 
    <body> 
        <a href="#">Test</a>
    </body> 
</html>

ajax.html

It works!

test.js

$(document).ready(function(){
    $( 'a' ).click(function(){
        $.post( '/ietest/ajax.html', function( data ) {
            alert( data );
        });
    });
});

Try it here: http://t1318.greatnet.de/ietest/test.html

A: 

If its local (localhost), then for security reasons you have to have the full path.

adardesign
Tried it here with `http://192.168.0.5/ietest/ajax.html`. Other browsers do it, IE still the same.
rallex
But it is not localhost... try it here: http://t1318.greatnet.de/ietest/test.html
rallex
A: 

I can reproduce it; to "reset" the page to get the error again, just add a random GET parameter to the OP's URL.

I'm getting a different error in IE8: The object does not support this method or property (or its english equivalent, it's a german IE)

It's fired by this line in jquery.js: return new window.XMLHttpRequest();

Interestingly, it's in the following block:

/*
        timeout: 0,
        data: null,
        username: null,
        password: null,
        traditional: false,
        */
        // Create the request object; Microsoft failed to properly
        // implement the XMLHttpRequest in IE7 (can't request local files),
        // so we use the ActiveXObject when it is available
        // This function can be overriden by calling jQuery.ajaxSetup
        xhr: window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject) ?
            function() {
                return new window.XMLHttpRequest();
            } :
            function() {
                try {
                    return new window.ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {}
            },

I'm baffled and don't understand the logic behind it, though.

Anybody got any ideas?

Could it be the doctype? Can you try a different (HTML) one?

Pekka
Tried HTML 4.01 and HTML 5, no difference, also without any doctype.
rallex
+1  A: 

From the post on jquerys forum here, you have to have the content type meta as the first item in your head tag.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de"> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="/ietest/jquery.js"></script> 
        <script type="text/javascript" src="/ietest/test.js"></script>  
    </head> 
    <body> 
        <a href="#">Test</a>
    </body> 
</html>
Gordon Tucker
Really solved the problem, thanks!
rallex
I want to upvote this question AND answer one quintillion times AND bake each of you a cake. This was KILLING ME. I don't think I'd have ever figured it out without you.
Chris