1
const URL = "url";
const key = "abc-abc-abc";

  const getData = async () => {
    const res = await fetch(URL, {
    method: "GET",
    headers: {
      "X-Auth-Token": key,
      "Access-Control-Allow-Origin": "*",
    },
  });

  console.log(await res.json());
};
getData();

I am getting the errors -> GET url 401 -> Uncaught (in promise) SyntaxError: Unexpected end of input const res = await fetch(URL, { at getData()

6
  • 1
    The 401 would mean that your auth token is invalid, as long as you don't also get a cors policy error. But I tried with an unprotected api endpoint and this code should work, so it must be the auth method or the key. Commented Sep 1, 2022 at 7:07
  • why you putting await in console.log(await res.json()); your res will always awaited untill fetch call is completed. Commented Sep 1, 2022 at 7:08
  • @KiwiRupela Please send me the correct code and I should use get request only Commented Sep 1, 2022 at 8:45
  • @TD3V I reverified the token it's 100% correct and ya i should perform GET request only Commented Sep 1, 2022 at 8:45
  • What have you tried to resolve the problem? Did you check whether the API works as expected? Commented Sep 5, 2022 at 12:21

1 Answer 1

-1

Well the code was perfectly fine. The problem was with my chrome browser

In the command prompt
**First type**
cd "C:\Program Files\Google\Chrome\Application"  
**Next**
chrome.exe --disable-web-security --user-data-dir="c:/ChromeDevSession"
Sign up to request clarification or add additional context in comments.

2 Comments

That sounds like a horrible idea. How should anybody else use your website?
This is not my personal project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.