strangeQuirks
strangeQuirks

Reputation: 5940

Axios get request to Laravel endpoint from next.js

I have the following request to my laravel endpoint:

axios.get('http://localhost:8000/auth/login', {})
            .then(function (response) {
                console.log(response);
                return {};
            })
            .catch(function (error) {
                return {}
            });

And my laravel endpoint set up as:

public function index() {
        var_dump('login called.');die;
        return response()->json(
            [],
            200
        );
    }

I Started my nextjs server (port 3000) and laravel server(8000), and when i browse to localhost:8000/auth/login in my browser I see "login called". however when I do that axios call, I get a status 200ok but no response data.

Request URL:http://localhost:8000/auth/login
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8000
Referrer Policy:no-referrer-when-downgrade

Any idea what I am doing wrong?

Upvotes: 1

Views: 1680

Answers (1)

Leo
Leo

Reputation: 7420

Nothing is wrong with your code you are getting the response correctly, you see "login called" because you are accessing from a browser, therefore a browser has the cappability to render the html and you can see that.

But that axios call expects some json in return. If you tweak the response a bit:

public function index() {

        return response()->json(
            ['data' =>'Log in called'],
            200
        );
    }

and if you twak axios response a bit

axios.get('http://localhost:8000/auth/login', {})
            .then(function (response) {
                console.log(response.data);
                return {};
            })
            .catch(function (error) {
                return {}
            });

Inspect element open console and you will see 'Log in called'

Upvotes: 1

Related Questions