Reputation: 69
I am trying to write a javascript and execute it in a Html file. I can run the Javascript file on my own computer without problems, but when I add it in a Html file and run it in a browser I got an Cors Error. I added the mode: 'no-cors'. After that, I got a 'SyntaxError: Unexpected end of input'. I can still only run the Javascript part so I am completely lost in trying to find the syntaxerror. Does anyone have a clue?
<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>
Upvotes: 1
Views: 2240
Reputation: 393
Try this. your error will be solved. but for cors, setting cors will not solve your problem . check comment
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)
});
Upvotes: 1