AJAX在Firefox上有效,但在Chrome上无效 [英] AJAX works on Firefox but not on Chrome

查看:151
本文介绍了AJAX在Firefox上有效,但在Chrome上无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有这个HTML代码:

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>

和此javascript代码:

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;
                        }
            });
        }
    });
});

上面的代码使我可以在div内容中加载3个完全没有标签的不同xml文件.可以在Firefox上正常运行,但不能在Chrome(Wind或Ubuntu Chromium)上运行

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)

推荐答案

将if条件更改为if(href) { .... 您正在检查值是不确定的还是空字符串,如果您执行if(href) { ...,则两者都将得出false.

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) { ....

我现在正在Windows中使用chrome,并且您的代码似乎可以正常工作(当然,由于没有文件,它会引发404错误).

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).

如果那不能解决您的问题,请尝试打开Chrome开发者工具,并检查在网络"中是否看到ajax请求.

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

更新:添加了 jsfiddle ,并且在ajax成功方面显示了空白警报

Update: added jsfiddle with a blank alert on ajax success

这篇关于AJAX在Firefox上有效,但在Chrome上无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆