在尝试使用jsonResponse实现来自Codecademy的fetch API的一些代码后,我有了这个SyntaxError :我认为这只是因为jsonResponse必须被分配一个变量,因为它是一个常量:
https://www.accesstheflock.io/matchTable/. PayTheBirdDog : SyntaxError: Unexpected identifier 'await'. const declared variable 'jsonResponse' must have an initializer. l:234 c:0
<script>
/*
response.headers.get('Content-Type') + response.url + response.status
fetch('https://www.accesstheflock.io/matchTable/DoggingFistfuls')
.then(
response => {
alert(respons
e.json());
},
rejection => {
alert(rejection.message);
}
);
*/
const doggingFistfuls = async() => {
try {
fetch('https://www.accesstheflock.io/matchTable/DoggingFistfuls');
if (response.ok) {
const jsonResponse await response.json();
} catch(error) {
alert(error);
}
//if
}
}
</script>发布于 2020-11-17 12:29:45
您需要使用await调用fetch,并将其赋给一个变量:
const doggingFistfuls = async() => {
try {
const response = await fetch('https://www.accesstheflock.io/matchTable/DoggingFistfuls');
if (response.ok) {
const jsonResponse = await response.json();
} catch(error) {
alert(error);
}
//if}}
https://stackoverflow.com/questions/64869339
复制相似问题