0

I am setting up authentication using Laravel (Laravel Framework version 5.8.4) as a REST API, but when I make a post request using Axios, I get back an empty string.

Here is my code in Laravel: "login" endpoint in my main controller:

 class MainController extends Controller
{

public function login(Request $request){

$data = [
'message' => 'yo'
];

return Response::json($data, 200);

 }
}


Here is my Axios code (from Vue.js method):

methods: {

      submitRegistration: function() {

    axios.post('http://envelope-api.test/api/auth/login', {
    name: this.form.name,
    email: this.form.email,
    password: this.form.password,
    password_confirmation: this.form.password_confirmation
  })
  .then(function (response) {
      console.log("here's the response")
      console.log(response);
    })
  .catch(function (error) {
    console.log(error);
  });

    },
  }

Here is the response from Postman (it works!)

{
    "message": "yo"
}

Here is the response from my axios request in console (empty string, where's the data?) :

{data: "", status: 200, statusText: "OK", headers: {…}, config: {…}, …}
Tony Brackins
  • 81
  • 2
  • 6

2 Answers2

0

To get data from axios you should use response.data, not just response.

Edit: Try to respond with the helper.

response()->json($data);

Benjamin Beganović
  • 1,070
  • 1
  • 8
  • 12
0

I've got this figured out. Thanks for your help.

I had this chrome extension installed to allow CORS (Cross Origin Resource Sharing) so I could do API requests from localhost (apparently, not needed for Postman?).

I turned it off and installed it locally on Laravel using this post (answer from naabster)

After I installed this way, it worked regularly.

Tony Brackins
  • 81
  • 2
  • 6