0

I am trying to save a simple registration form to server using fetch but getting response false.

apiDataSave = () => {
         fetch('<myapi>', {  
            method: 'POST',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                name : "abc",
                no : "1234567890",
                password : "123",
                email : "abc@gmail.com"  
            })
          })
          .then(function(response) {
              return response.json();
            })
          .then(function(data) {
              alert(`response json is: ${JSON.stringify(data)}`)
          })

}

I am getting alert as follows:

response json is: {"status":false,"data":[]}

The expected result alert is:

response json is: {"status":true,"data":"1"} // number of records entered in database

Neha
  • 175
  • 1
  • 3
  • 18

1 Answers1

0

The problem was that the Content-Type was not 'application/json' but following:

'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'

I got the answer here: stackoverflow answer

Neha
  • 175
  • 1
  • 3
  • 18