Diego Quirós
Diego Quirós

Reputation: 977

AJAX works on Firefox but not on Chrome

OK, I have this HTML code:

<head>
    <script type="text/javascript" src="js/jquery.1.3.2.min.js" ></script>
    <script type="text/javascript" src="js/jquery-ui.min.js" ></script>
    <script type="text/javascript" src="js/funciones.js" ></script>
</head>

<body> 

    <div>
    <a><div id= "0" class="Button" href="./es/uno.xml">uno</div></a>
    <a ><div id= "1" class="Button" href="./es/dos.xml">dos</div></a>
    <a ><div id= "2" class="Button" href="./es/tres.xml">tres</div></a>
    </div>

    <br/>
    <br/>
    <br/>

    <div id="content" class="content">

    </div>

</body>
</html>

and this javascript code:

 $(document).ready(function() {
        $('.Button').click(function() {
        var href = $(this).attr('href');

        if( typeof href != "undefined" && href != ""){
            $.ajax({
                url: href,
                dataType: 'text',
                success: function(data) {
                            //alert($(data).contents().contents().text());
                            $('#content').html(data);
                            ultXML = href;
                        }
            });
        }
    });
});

The above code let me load 3 diferents xml files with no tags at all in the div content. It works OK on Firefox but it doesn t work on Chrome (Neither Wind or Ubuntu Chromium)

Upvotes: 1

Views: 2083

Answers (2)

Diego Quir&#243;s
Diego Quir&#243;s

Reputation: 977

I finally discover what was the stupid newbie error! The code has nothing wrong is working well. The problem was when I was testing it locally:

file:///home/.../index.html (navigation browser bar)

FF can find the path of ./es/uno.xml but (i dint know why) Chrome can not. So it replace the DIV content with nothing.

It works ok if I put the files in a server to access it in the way:

http://example.com

Thanks Everybody!

Upvotes: 0

Chris
Chris

Reputation: 333

Change the if conditions to if(href) { .... You are checking if the value is undefined or an empty string, both will evaluate to false if you do if(href) { ....

I am using chrome in windows right now, and your code seems to work (of course it throws me a 404 error cause i don't have the files).

If that didn't solve your problem, try to open the Chrome developer tools and check if in "Network" you see the ajax request.

Update: added jsfiddle with a blank alert on ajax success

Upvotes: 2

Related Questions