0

I am trying to develop an application using reactjs as front end framework and laravel 5.6 as back end framework. I am trying to send AJAX request like below

import Auth from '../services/Auth';

var address_data = {
        name        :'foysal',                    
        address     :'foysal',
        telephone_no:'foysal',
        email       :'foysal',
}



fetch('http://127.0.0.1:8000/api/addresses/store/',address_data, {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + Auth.getToken(),
        },
        body: JSON.stringify()
    })
    .then((response) => response.json())
    .then((responseData) => {
        console.log(responseData);
    })

I am getting below errors

enter image description here

enter image description here

5
  • 1
    body: JSON.stringify() do you mean body: JSON.stringify(address_data) ? Commented Jun 5, 2018 at 8:00
  • 1
    I can see browser is initiating GET and not POST. Please refer developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch on how to use fetch otherwise use simple ajax request Commented Jun 5, 2018 at 8:03
  • @AnatolyStrashkevich, sorry I could not understand. Commented Jun 5, 2018 at 8:33
  • @SureshPrajapati, Why it is sending GET request ? I think the issue is here ? What should I correct ? Thanks. Commented Jun 5, 2018 at 8:46
  • 1
    @abuabu Let me know if you are facing any issue once you try my answer Commented Jun 5, 2018 at 9:18

1 Answer 1

1

Try changing your fetch request to initiate POST action verb like this

fetch('http://127.0.0.1:8000/api/addresses/store/', {
  method: 'POST',
  headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + Auth.getToken(),
  },
  body: JSON.stringify(address_data)
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
Sign up to request clarification or add additional context in comments.

Comments

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.