【发布时间】:2017-09-27 11:26:15
【问题描述】:
我的本地机器中有一个 JSON 文件,其中包含多个属性列表。 一个sn-p的代码如下,供参考;
{
"properties": [
{
"id":"prop1",
"type":"House",
"bedrooms":3,
"price":650000,
"tenure":"Freehold",
"description":"something something etc...",
"location":"Petts Wood Road, Petts Wood, Orpington",
"picture":"images/prop1pic1small.jpg",
"url":"prop1.html",
"added": {
"month":"January",
"day":12,
"year":2016
}
}
}
然后我用 AJAX 编写了一个脚本,允许我根据不同的属性从 JSON 文件中检索内容。
btnType.addEventListener('click', function() { //search by house type
var searchText = document.getElementById("search").value;
$.ajax ({
url: 'properties.json',
dataType: 'json',
type: 'get',
cache: 'false',
success: function(data) {
var noResults = 0; // this variable is used to capture if no results are returned
var output = '<ul>'; //the final output is concatenated to one variable to be displayed
var len = data.properties.length;
for (i=0; i < len; i++) { //condition is checked with the JSON data and returns only the valid results
if (data.properties[i].type == searchText) {
output += '<section id = "oneResult">';
output += '<p id = "resultID">Property Code: ' + data.properties[i].id + '</p>';
output += '<img src = "'+data.properties[i].picture+'">';
output += '<p id = "resltDesc">'+data.properties[i].description+'</p> <a href = "'+data.properties[i].url+'" target = "_blank">See More</a> <br>';
output += '<p>Going for $'+data.properties[i].price+'</p> <br>';
output += '</section><br><br>';
noResults++;
}
}
output += '</ul>';
$('#update').html(output); //final output displayed
if (noResults == 0) {
$('#update').html("No results found");
}
}
});
});
我想知道的是,当我在谷歌浏览器(最常用的浏览器)上运行它时,没有出现结果,开发者控制台显示以下错误;
XMLHttpRequest cannot load file:///D:/IIT/LVL5%20SEM1/Adv.%20Client%20side%20Web/FINAL%20CW/FINAL%20FILES/2015235-w1608508-CW2-ACWD/properties.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
但是当我在 Firefox 上运行它时,完全相同的文件运行时没有打嗝。 这与我的代码有关还是与浏览器相关?
编辑 - 由于从外观上看这是一个特定于浏览器的安全功能,现在更有趣的是要知道它为哪些问题提供了安全性?
【问题讨论】:
-
谢谢你。我正在寻找这个答案,但我认为我的查询并没有通过它。那么我是否理解它是 chrome 中的一项安全功能?如果是这样,它实际上提供了什么样的保护?我编辑了问题以避免重复
标签: html json google-chrome security firefox