Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery中的Ajax不能从本地文件工作_Jquery_Html_Ajax_Local - Fatal编程技术网

Jquery中的Ajax不能从本地文件工作

Jquery中的Ajax不能从本地文件工作,jquery,html,ajax,local,Jquery,Html,Ajax,Local,我用简单的ajax创建了简单的html文件 index.html: <html> <head> <meta http-equiv="Content-Type" content="text/html; Charset=UTF-8"> <script type="text/javascript" src="jquery.js"></script> </head> <body> <div id="cont

我用简单的ajax创建了简单的html文件

index.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; Charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
    <div id="content"></div>

    <script>
        function show()
        {
                $.ajax({
                url: "2.html",
                cache: false,
                success: function(html){
                    $("#content").html(html);
                }
            });
        }

        $(document).ready(function(){
            show();
            setInterval('show()',1000);
        });
    </script>

</body>
</html>

函数show()
{
$.ajax({
网址:“2.html”,
cache:false,
成功:函数(html){
$(#content”).html(html);
}
});
}
$(文档).ready(函数(){
show();
setInterval('show()',1000);
});
文件2.html与文件index.html位于同一目录中。并包含例如:

 <p>ssss hkl jh lkh <b>d1111</b></p>
ssss hkl jh lkh d1111


当我在Web服务器上运行index.html时,一切正常。但是如果您在计算机上以本地文件的形式运行文件index.html,ajax将无法工作。如何修复?

一些浏览器实施了强大的安全措施,以防止下载的网页访问文件系统上的任意文件


切换到安全性较弱的浏览器(我认为Firefox允许通过XHR访问本地文件)或停止尝试运行没有HTTP的网站。

如果您正在检查Chrome,这是一个已知的问题。使用XAMPP运行本地Web服务器,并测试ajax调用


检查此票据:

是否在
index.html
旁边包含
jquery.js
?您是否使用Chrome?Chrome不允许使用本地ajax。当然,使用Python会更快。在终端中:
cd/path/to/project/folder
。(OSX,Py 2.7x)或者您可以使用node.js.>npm安装-g http server>cd/path/to/project/folder>http服务器还带有chrome,命令行如下:
chrome——允许从文件访问文件