XMLHttpRequest cannot load file:///C:/Users/hamma/

2019-01-29 14:24发布

<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.1.4.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#div1").load("rao.txt");
    });
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>

i want to change the content of div1 with some text or some other webpage without reloading the default page.

I get the following error message in the console:

XMLHttpRequest cannot load file:///C:/Users/hamma/Desktop/rao.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

2条回答
成全新的幸福
2楼-- · 2019-01-29 15:04

Copy the file "rao.txt" into your project folder. Setup a localserver. Use wamp/xamp for setting localserver. Access the file like http://localhost/project_folder/path_to_rao.txt

查看更多
仙女界的扛把子
3楼-- · 2019-01-29 15:05

According to your console output:

XMLHttpRequest cannot load file:///C:/Users/hamma/Desktop/rao.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

That means that you are trying to run the file without a server i.e. file:///[...] and for security reasons you can't run AJAX requests like that.

You need to setup a server and run the page through that to send requests.

More Help

If you are using Chrome, try this.

Reading Material

Same-Origin-Policy

CORS

查看更多
登录 后发表回答