下面的ajax-getJSON(.htm)示例實際上會在Internet Explorer上從我找到示例的網站上運行,但當我複製示例代碼並嘗試在我的工作站(win10)上本地運行示例時,它僅適用於Microsoft Edge。在IE和Chrome我得到這個錯誤信息 -ajax-getJSON示例在我的win10工作站上可以在Microsoft Edge上運行,但不能運行Internet Explorer或谷歌瀏覽器
從網頁錯誤消息:拒絕訪問
所有子文件夾具有管理/本地用戶完全控制...是否有此錯誤的解決方法,所以我可以在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>
這裏是
[{
"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"
}]
你只是直接從你的電腦打開HTML文件?如果我記得,出於安全原因,chrome不會讓html頁面通過ajax調用從計算機獲取文件。請參閱https://stackoverflow.com/questions/4819060/allow-google-chrome-to-use-xmlhttprequest-to-load-a-url-from-a-local-file –