Ajax in Jquery does not work from local file

左心房为你撑大大i 提交于 2019-11-26 05:54:29

问题


I created simple html file with simple ajax.

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>

File 2.html located in the same directory as the file index.html. And contains for example:

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

When I run the index.html on the webserver, everything works. But if you run file index.html on the computer as a local file ajax not working. How to fix it?


回答1:


This is a known problem with Chrome, if you are checking on it. Use XAMPP to run a local webserver, and test your ajax call.

Check this ticket: https://code.google.com/p/chromium/issues/detail?id=40787




回答2:


Some browsers implement strong security measures to prevent downloaded webpages from accessing arbitrary files on the file system.

Switch to a browser with weaker security (I think Firefox permits access to local files via XHR) or stop trying to run a website without HTTP.



来源:https://stackoverflow.com/questions/17947971/ajax-in-jquery-does-not-work-from-local-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!