-1

I'm using the following code but I'm getting the error "Unexpected token < in JSON at position 0" Why and I getting it and how can I fix this?

async function sendURL(urls) {
  const res = await fetch("/ffmpegserver/upload/urls", 
  { method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, 
  body: JSON.stringify({ urls: urls}) });
  const json = await res.json();
  return json;
}


window.onload = async () => {
  const result = await sendURL([
    "http://s5.qhres.com/static/465f1f953f1e6ff2.mp3",
    "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3"
  ]);
  console.log(result);
}
Zack Lee
  • 2,784
  • 6
  • 35
  • 77
  • Does this answer your question? ["SyntaxError: Unexpected token < in JSON at position 0"](https://stackoverflow.com/questions/37280274/syntaxerror-unexpected-token-in-json-at-position-0) – Ivar Oct 24 '20 at 00:02

1 Answers1

1

My money says the response isn't valid json, and possibly HTML instead (hence the <), possibly because the URL is incorrect, meaning that you get the body of a 404 page which might not be accepting application/json.

You can check this with, instead of const json = await res.json();, try:

const body = await res.text();
console.log(body);