-1

I have one API made in PHP, but i want consume with Reactjs using fetch, but i can not get, someone can help me?

handleSubmit = event => {

  event.preventDefault();
  const email = document.querySelector(".email").value;
  const senha = document.querySelector(".password").value;

  alert(JSON.stringify({email, senha}));

  const res = {
     method: 'POST',
     mode:'no-cors',
     body: JSON.stringify({email,senha}),
     headers: new Headers({
       'Content-type': 'application/json',
       'Authorization': 'token here',
     }),
   };
   fetch('http://rhvagas-api/login', res).then(res => {})
   .then(result => {
      console.log(res);
      return result.json();
   })
 }

Error: enter image description here

POSTMAN HERE: enter image description here

  • 2
    There is also a discrepancy between the URI used in fetch (http://rhvagas-api/login) and the one displayed in Postman (http://rhvagas-api/user/login). Could that be your issue? – Thomas Hennes Feb 11 '19 at 13:26

1 Answers1

-1

Try

handleSubmit = event => {

  event.preventDefault();
  const email = document.querySelector(".email").value;
  const senha = document.querySelector(".password").value;

  alert(JSON.stringify({email, senha}));

  const res = {
     method: 'POST',
     mode:'no-cors',
     body: JSON.stringify({email,senha}),
     headers: new Headers({
       'Content-type': 'application/json',
       'Authorization': 'token here',
     }),
   };
   fetch('http://rhvagas-api/login', res).then(res => res.json())
   .then(result => {
      console.log(result);
      // work with the result

   })
 }
ManavM
  • 2,918
  • 2
  • 20
  • 33
  • Return in console.log : SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data[Learn More] – Arthur Luiz Feb 11 '19 at 13:38
  • Two downvotes an upvote and the answer gets accepted...I'm not sure if I should remove the post or not. Can someone tell me what was unsatisfactory so I can just edit it – ManavM Feb 11 '19 at 21:34