下面的ajax (.htm)示例实际上将从我找到示例的网站上运行在Internet上,但是当我复制示例代码并尝试在我的工作站(win10)上本地运行该示例时,它只在Microsoft上工作。在IE和Chrome上,我收到一条错误消息--
来自网页错误的消息:拒绝访问
所有子文件夹对管理员/本地用户都有完全控制.有解决这个错误的方法吗?这样我就可以在IE和/或Chrome上运行它了?是否需要对任何内容进行重构,以便将其作为.htm (.html)文件运行?
<html>
<head>
<title>Test Json thing</title>
<style>
body
{
background-color:#FAF9CF;
}
</style>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$("#btn382").click(function(){
/* set no cache */
$.ajaxSetup({ cache: false });
$.getJSON("car-saleX.json", function(data){
var html = [];
/* loop through array */
$.each(data, function(index, d){
html.push("Manufacturer : ", d.Manufacturer, ", ",
"Sold : ", d.Sold, ", ",
"Month : ", d.Month, "<br>");
});
$("#div381").html(html.join('')).css("background-color", "orange");
}).error(function(jqXHR, textStatus, errorThrown){ /* assign handler */
alert("error occurred! " + jqXHR + " *" + textStatus + "* " + errorThrown);
});
});
});
</script>
</head>
<body>
<form name="form1" method="post" id="form1">
<div id="example-section38">
<div>Car sale report</div>
<div id="div381"></div>
<button id="btn382" type="button">Click to load car sale report (json type)</button>
<a href="car-saleX.json" target="_blank">Original car sale report</a>
</div>
</form>
</body>
</html>下面是示例.json数据文件(car-saleX.json)
[{
"Manufacturer": "Toyota",
"Sold": 1200,
"Month": "2012-11"
}, {
"Manufacturer": "Ford",
"Sold": 1100,
"Month": "2012-11"
}, {
"Manufacturer": "BMW",
"Sold": 900,
"Month": "2012-11"
}, {
"Manufacturer": "Benz",
"Sold": 600,
"Month": "2012-11"
}, {
"Manufacturer": "GMC",
"Sold": 500,
"Month": "2012-11"
}, {
"Manufacturer": "HUMMER",
"Sold": 120,
"Month": "2012-11"
}]发布于 2017-10-10 19:09:32
您应该在本地主机服务器上运行代码。因此,您至少需要安装Apache和PHP。然后简单地从控制台运行命令php -S localhost:8000
https://stackoverflow.com/questions/46674047
复制相似问题