[英]Parsing JSON file to use in JavaScript
我正在嘗試解析JSON文件,以便可以使用JavaScript訪問其數據。 這是JSON文件的示例:
"Veg": {
"French beans 300g": 81969,
"Bunched beetroot": 78272,
"Squash box": 45450,
"Bunched carrots 500g": 90435,
"Courgettes x3": 17824,
"Fennel x1": 14816,
"Shiitake mushrooms 150g": 5166,
"Spring onions 125g": 65034,
"Red peppers x2": 11671,
這是我正在使用的JavaScript:
<script>
var sourceData;
// Initial data load
fetch("data.json", {
method: "GET",
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
})
}).then(function (body) {
return body.json();
}).then(function (data) {
sourceData = data;
});
</script>
但我收到此錯誤:
Failed to load resource: Preflight response is not successful
和
Fetch API cannot load <file path> Preflight response is not successful
嘗試使用
parseVariable = JSON.parse(JsonVariable);
console.log(parseVariable);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.