Hello I'm trying to process an API call. I've written the API request code in html in brackets. I'm a novice. Getting the message Uncaught SyntaxError: Unexpected token '<' Here's the code, I've left out actual http link as post kept getting rejected: (And I've substituted the APi Key) <!doctype html> <html> <script> //GET var burl = ' '; var endpoint = 'status?'; var parameters = 'api_key=N#####VIlX8zb!9ZL'; var url = burl + endpoint + parameters; request ('GET' ,url). then((r1) => { //SPECIFIY THE CODE TO HANDLE API RESPONSE console.log(r1); }). catch((err) => { console.log(err); }) function request (method, url) { return new Promise(function (resolve, reject) { var xhr = new XMLHttpRequest(); xhr.open(method, url); xhr.onload = resolve; xhr.onerror = reject; xhr.send(); }); } </script> </html> Code (markup):
Does it say WHERE this "unexpected token" occurs? WHAT is saying unexpected token, your editor? Though honestly, this is a great example of why I dislike this "promise" BS with the needlessly pointlessly cryptic trash arrow functions... and how this new methodology pisses on state/error handling. But I don't see anything in that code that should be saying that as an error.
This Unexpected token error occurs mostly when a function expects JSON input but gets HTML input. E.g JSON.parse(res); If the res variable comes as an HTML string '<>'. You will get that error. Some functions use JSON only functions internally so..... Like @deathshadow mentioned, temporarily do away with the promise and console.log the raw output from the Ajax request.
What is the data you want to fetch? This seems to be the code straight out from a tutorial you copied from somewhere. Try something more simple to see what error is: <!doctype html> <html> <script> //GET var burl = ' '; var endpoint = 'status?'; var parameters = 'api_key=N#####VIlX8zb!9ZL'; var url = burl + endpoint + parameters; fetch(url).then(function(response) { return (response.text()); }).then(function(responseText) { console.log(responseText); // do something with the response text, export it to other function }).catch(function(err) { console.log(err); }); </script> </html> Code (markup):
That's an accepted (though ugly) optional formatting for promises, hence the 'then' on the next line. It's effectively the same as: request ('GET' ,url).then((r1) => { //SPECIFIY THE CODE TO HANDLE API RESPONSE console.log(r1); }).catch((err) => { console.log(err); }) Code (markup): And part of why I hate that promise junk. IT's cryptic, painfully daisy-chains the request logic, when used with the garbage "request" idiocy removes your ability to properly handle error status and provides ZERO mechanism for handling response codes... ... and it's why so many sites that rely on it go off to never-never land if there's a problem with the request in an endless "loading" loop because they don't even make provisions for something going wrong. Which is why I'd take this entire section of code, throw it in the trash, and just use good old-fashioned XMLHttpRequest. Might be more code, but it's more reliable, let's you leverage status, has hooks for monitoring the progress, etc, etc, etc. Fetch/request/etc are incompetent trash and a total waste of time and effort, and rank amongst the recent ECMAScript changes that reek of people who don't have enough experience writing real software now being in charge of the language spec.