我看到上面提到了上面的问题,但是即使在提到它们之后我也无法找到适合我的方法,因此我冒昧地为这个问题开了一个新帖子.
我有一个包含以下代码的getjson.html文件
<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>
Run Code Online (Sandbox Code Playgroud)
json_data.txt包含以下内容,
{
"firstName": "John",
"lastName": "Doe",
"age": 25
}
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中打开getjson.html文件时,它会出错:
"XMLHttpRequest无法加载file:// ..... Access-Control-Allow-Origin不允许使用Origin null."
有人请建议一个简单的解决方案,我怎么能让这件事成为可能.
PS:我正在写一个网络应用程序.