0

I want to change an API call (external source, no chance to change something on the API side) from PHP to Javascript (Learning purposes).

Because of the cross-origin, I use fetch(). When I run my script, I get an Unexpected end of input error and can't figure out why.

function postData(url = '', data = {}) {
  var headers = new Headers();
  headers.set('Authorization', 'Basic ' + window.btoa("user" + ':' + "pass"));
  return fetch(url, {
    method: 'POST',
    mode: 'no-cors',
    cache: 'no-cache',
    credentials: 'include',
    headers: {
      'Content-Type': 'application/json'
    },
    redirect: 'follow',
    referrer: 'no-referrer',
    body: JSON.stringify(data),
  }).then(response => response.json()).catch(error => console.error(error));
}

postData('https://www.api-endpoint.com/cat1/api/search?', {
  "searchID": "710",
  "isTagged": true
}).then(data => console.log(JSON.stringify(data))).catch(error => console.error(error));

How can I identify the problem with this code? It seems the Authorization is okay. I implemented the search parameters (searchID and isTagged) as described on the manual from the API Dev.

Thanks in advance!

2
  • Which specific action is causing the error? Commented Aug 30, 2019 at 12:52
  • When setting the response. I guess it is because of the empty String as Quentin stated in his answer Commented Aug 30, 2019 at 12:56

1 Answer 1

4

You said mode: 'no-cors', which disables everything which requires CORS permission.

Since reading data across origins requires CORS permission, there is no data.

Trying to parse an empty string as JSON results in the unexpected end of input because the input ended before there was any JSON.

(Note that other things which require CORS permissions, and which you are trying to do, include setting the content-type to JSON and including credentials).

Sign up to request clarification or add additional context in comments.

10 Comments

@youwhatmate — So the server isn't using CORS to grant you permission. stackoverflow.com/a/35553666/19068
You can't fetch data across origins without CORS. You definitely need a proxy on your same origin.
"I tried the browser extension" — The answer I pointed you to says: "They also tend to work only with simple requests (failing when handling preflight OPTIONS requests).". Since you are doing plenty of stuff that needs a preflight, that is to be expected.
"jsonp" — Which says "Bob could also provide the data using a hack like JSONP" … did you ask if the API you were using supported JSONP? And since you can't (a) make a POST request (b) set credentials or (c) set headers with JSONP … it wouldn't be remotely equivalent anyway.
"the other solution" — Which other solution? The proxy? Not writing a web app? Talking to the people running the service you are trying to POST to?
|

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.