Wednesday, March 1, 2017

Ajax request to API not working in fresh installation of Laravel 5.4

Running the following commands:

$ laravel new app

$ composer install

$ php artisan make:auth

Fill new DB info.

$ php artisan migrate

$ yarn install

Hit /register and make a new user, now I'm logged in into Laravel. I open the JavaScript console and I write the following:

> axios.get('/api/user').then(res => console.log(res.data));

Following I get: GET http://ift.tt/2lb3umT 401 (Unauthorized)

My headers are:

GET /api/user HTTP/1.1 Host: 127.0.0.1:8000 Connection: keep-alive Authorization: QmU4zkAA5N9BVoZsJEX4cQIn3UHwzNmFEwZkl8vy X-XSRF-TOKEN: eyJpdiI6ImFcL1JYeFZnV1piVG5jRFZYWnFKVUpnPT0iLCJ2YWx1ZSI6ImczYmxMZW1Fa2xPaVRwUVQyUTJ0Wm1UNHo0NWNxZkRJazFKY3o3R05XTE9KcVNyMDlzOE80NitpZ1FBcjJOUXJGYXl1bnJyN2hFMksyR2RcL1RobWNcL0E9PSIsIm1hYyI6IjY5NGUzNzc1MjNmODdiYmJlYzI2YzI4YWQzMGQwMWY2MWVjYmUzZThkNDZjYWEzM2MxMzFlNmRkMzRlY2Q0OTcifQ== X-CSRF-TOKEN: QmU4zkAA5N9BVoZsJEX4cQIn3UHwzNmFEwZkl8vy User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 Accept: application/json, text/plain, */* X-Requested-With: XMLHttpRequest Referer: http://ift.tt/1nHH4WY Accept-Encoding: gzip, deflate, sdch, br Accept-Language: es-ES,es;q=0.8,en;q=0.6,pt;q=0.4 Cookie: XSRF-TOKEN=eyJpdiI6ImFcL1JYeFZnV1piVG5jRFZYWnFKVUpnPT0iLCJ2YWx1ZSI6ImczYmxMZW1Fa2xPaVRwUVQyUTJ0Wm1UNHo0NWNxZkRJazFKY3o3R05XTE9KcVNyMDlzOE80NitpZ1FBcjJOUXJGYXl1bnJyN2hFMksyR2RcL1RobWNcL0E9PSIsIm1hYyI6IjY5NGUzNzc1MjNmODdiYmJlYzI2YzI4YWQzMGQwMWY2MWVjYmUzZThkNDZjYWEzM2MxMzFlNmRkMzRlY2Q0OTcifQ%3D%3D; laravel_session=eyJpdiI6ImRtZzdPUElaY1dcL1hLWHJPZVkwaW5RPT0iLCJ2YWx1ZSI6IitrVm9GOW5TQTdWZldJY3VlWU5EeWVBRitIVjVrYXpkWGhmWmJlWjJrTmlCWVZ0VUkwdEFrY3VoU2ZCMnRKMnlCV29pQ3NVa0ROdHEyaXlLMWhuWTd3PT0iLCJtYWMiOiJhMGE0ZTg5NTlkYzlkYmM2MjBhMjNmYTA2ZjUyYmE0N2RlMzVmNjA4Zjg3MDZhMDEzMDM1MGJjOWUzNzg5NDk3In0%3D

Anyone going through a similar situation?




via marcovega

Advertisement