error: SyntaxError: Javascript中输入的意外结束



我正试图编写一个javascript并在Html文件中执行它。我可以在我自己的计算机上运行Javascript文件而没有问题,但是当我将其添加到Html文件并在浏览器中运行时,我得到了一个Cors错误。我添加了模式:'no-cors'。在那之后,我得到了一个"SyntaxError: Unexpected end of input"。我仍然只能运行Javascript部分,所以我完全迷失在试图找到语法错误。有人知道吗?

<html>
<body>
<script>
var apiUrl = 'https://bulkfollows.com/api/v2';
// define the data to be sent to the API
var data = {
key: "key",
action: "balance"
};
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
mode: 'no-cors',
body: JSON.stringify(data)
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.log("Error: " + error));
</script>
</body>
</html>

试试这个。你的错误将得到解决。但是对于cors,设置cors并不能解决你的问题。检查评论

fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
mode: "no-cors",
body: JSON.stringify(data)
})
.then((response) => {
if (!response.ok) {
throw response; 
}
return response.json();
})
.then((data) => console.log(data))
.catch(function(error) {
console.log( error)
});

最新更新