Javascript Uncaught (in promise) SyntaxError: Unexpected token ' in fetch function
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35852192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught (in promise) SyntaxError: Unexpected token ' in fetch function
提问by Cassidy Williams
I have a couple JSON files that are structured like this (let's call this info.json):
我有几个结构如下的 JSON 文件(我们称之为 info.json):
{
'data': {
'title': 'Job',
'company': 'Company',
'past': [
'fulltime': [
'Former Company'
],
'intern': [
'Women & IT',
'Priority 5'
]
],
'hobbies': [
'playing guitar',
'singing karaoke',
'playing Minecraft',
]
}
}
And in a separate JavaScript file, I have a function that looks like this:
在一个单独的 JavaScript 文件中,我有一个如下所示的函数:
function getJSONInfo() {
fetch('info.json').then(function(response) {
return response.json();
}).then(function(j) {
console.log(j);
});
}
And I keep getting this error when I run getJSONInfo()
:
当我运行时,我不断收到此错误getJSONInfo()
:
Uncaught (in promise) SyntaxError: Unexpected token '
What am I missing? I don't have a stray '
anywhere so I'm not sure what's wrong.
我错过了什么?我在'
任何地方都没有流浪,所以我不确定出了什么问题。
回答by Collin
You need to have double quotes for your attributes for valid json.
您需要为有效 json 的属性加上双引号。
You can use json validators such as http://jsonlint.com/to check if your syntax is correct.
您可以使用 json 验证器(例如http://jsonlint.com/)来检查您的语法是否正确。
Also, as shayanypn pointed out, "past" should be an object, rather than an array. You are trying to define "past" as an object literal but are using square brackets to denote an array.
此外,正如 shayanypn 指出的那样,“过去”应该是一个对象,而不是一个数组。您试图将“过去”定义为对象文字,但使用方括号表示数组。
回答by Shayan
you is invalid at all
你根本无效
1- you should use double quotes
1-你应该使用双引号
2- bad syntax of object attribute
2-对象属性的错误语法
"past": [
"fulltime": [
"Former Company"
],
"intern": [
"Women & IT",
"Priority 5"
]
],
it should bed
它应该睡觉
"past": {
"fulltime": [
"Former Company"
],
"intern": [
"Women & IT",
"Priority 5"
]
},
your valid json is
你的有效 json 是
{
"data": {
"title": "Job",
"company": "Company",
"past": {
"fulltime": [
"Former Company"
],
"intern": [
"Women & IT",
"Priority 5"
]
},
"hobbies": [
"playing guitar",
"singing karaoke",
"playing Minecraft"
]
}
}