XMLHttpRequest无法加载file:// ... Access-Control-Allow-Origin不允许使用Origin null [英] XMLHttpRequest cannot load file://... Origin null is not allowed by Access-Control-Allow-Origin

查看:134
本文介绍了XMLHttpRequest无法加载file:// ... Access-Control-Allow-Origin不允许使用Origin null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到上面提到了上述问题,但是即使在提到它们之后我也无法找到适合自己的方法,因此我冒昧地为这个问题开了一个新帖子。

I see that the above question was asked earlier, however even after referring them I wasn't able to figure out a way for me, hence I took the liberty to start a new post for this question.

我有一个包含以下代码的getjson.html文件

I have a getjson.html file containing the following code

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $.getJSON("json_data.txt",function(result){
      $.each(result, function(i, field){
        $("div").append(field + " ");
      });
    });
  });
});
</script>
</head>
<body>

<button>Get JSON data</button>
<div></div>

</body>
</html>

json_data.txt包含以下内容,

The json_data.txt contains the following,

{ 
  "firstName": "John",
  "lastName": "Doe",
  "age": 25
}

当我在浏览器中打开getjson.html文件时,它会出错:

When I open the getjson.html file in the browser, it gives an error:


XMLHttpRequest无法加载文件:// ...... Access-Control-Allow-Origin不允许使用Origin。

"XMLHttpRequest cannot load file://..... Origin null is not allowed by Access-Control-Allow-Origin."

有人请建议一个简单的解决方案,关于如何使这件事成为可能。

Someone Please suggest a simple solution regarding this about how could I make this thing possible.

PS:我正在编写一个Web应用程序。

P.S: I am writing a web app on go.

推荐答案

您是否直接打开HTML文件?该文件需要放在Web服务器上然后执行。通常,出于安全原因,浏览器不允许使用 file:/// 协议进行AJAX调用。

Are you directly opening an HTML file? The file needs to be placed on a web server then executed. Normally browsers don't allow file:/// protocol for AJAX calls for security reasons.

这篇关于XMLHttpRequest无法加载file:// ... Access-Control-Allow-Origin不允许使用Origin null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆