微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 无法从本地html页面ajax本地文件

根据Same Origin Policy,SOP不应该适用于file://协议,但为什么我的代码不起作用?我从本地系统运行此测试页面,并且我在与html页面相同的目录中有abc.txt.如果我将URL更改为http://www.google.com/,则它也不起作用.我不明白为什么,有人可以解释一下吗?

<!doctype html>
<html lang="us">
<head>
    <Meta charset="utf-8">
    <title>jQuery UI Example Page</title>
    <link href="css/sunny/jquery-ui-1.10.2.custom.css" rel="stylesheet">
    <script src="js/jquery-1.9.1.js"></script>
    <script src="js/jquery-ui-1.10.2.custom.js"></script>
    <script>
    $.support.cors = true;
    $(function(){
        $(".btn1").button().click(function(){
            $.ajax({

              url: "abc.txt"

            }).done(function(result) {
              alert("done");
              $(".content").html(result);
            }).fail(function() { 
                alert("error"); 
            });
        });

    });

    </script>
</head>
<body>

<button class="btn1">Click me!</button>

<div class="content"></div>

</body>
</html>

编辑:
控制台打印如下:

XMLHttpRequest cannot load
file:///C:/Users/yc/Desktop/jquery%20ajax%20testing/jquery-ui-1.10.2.custom/jquery-ui-1.10.2.custom/abc.txt.
Origin null is not allowed by Access-Control-Allow-Origin.

*它也不适用于Firefox,IE.

解决方法:

这不是一个错误,它是一个安全功能,你不能/不会在客户端的计算机上.

在chrome中,您可以通过在命令行上添加以下标志来禁用它

– 禁用网络安全

Firefox可能有类似的东西,但我不知道.这仅用于开发目的,您不能在应用程序中依赖此行为.

你应该真的只使用服务器……

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐