jQuery load() code not working in Google Chrome

蓝咒 提交于 2019-12-11 13:39:38

问题


i am stuck here with a little jQuery Problem! I am using this code to load the content of a html file (bio.html) into a div containter (content). This works in IE8 and Firefox 6, but not in Google Chrome!

Any ideas what i might be doing wrong? Heres the Code:

<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="./js/jquery.min.js"></script>
</head>

<div id="content">
    hi
</div>

<script type="text/javascript">

    $(document).ready(function(){   

        $('#content').load('./html/bio.html');

    });

</script>


回答1:


It works fine for me in chrome

http://sandbox.phpcode.eu/g/512ef/2

try to look at developer's inspect tool if you see any unexpected errors. Be sure you're in http:// or https:// protocol, file:// will not work




回答2:


.load() will not work in Chrome if you are loading files from your local system.

Putting it on a server will resolve the issue.




回答3:


Sounds like you are running this locally, is that correct? Chrome has some security features that prevent JQuery's load method to fail when run from a local harddrive. Try putting it on a server somewhere.




回答4:


The issue is just that .load() for local files is blocked by Chrome for security reasons. If you are using it on a server it works, given that all of the files originate from the same place.



To enable a working version locally, start Chrome with a command line flag enabled by trying:

In Mac OS X, quit Chrome, enter in Terminal:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files

In Windows, quit Chrome, enter in Command Prompt:

chrome.exe --allow-file-access-from-files

(Maybe you actually have to have the path... I don't think so. If so, you'll have to find it yourself.)

In Linux, quit Chrome, enter something like this into the terminal:

/usr/bin/google-chrome --allow-file-access-from-files


来源:https://stackoverflow.com/questions/7287480/jquery-load-code-not-working-in-google-chrome

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