Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Atef95's avatar

getting api response in the front end

Hey guys..

I'm wondering why when I fetch an API using ajax for example..

my response would be something like :

   success: (response) =>  {

                    if (response.status == 200) {
                     console.log('ok')

			}

In cas of VueJs with axios I have to add data before status

    success: (response) =>  {

                    if (response.data.status == 200) {
                     console.log('ok')

			}

why I should add that and what makes the difference ?

0 likes
4 replies
bobbybouwmann's avatar

It completely depends on the library you use and what the API returns.

For example, axios is a wrapper around ajax calls. It makes it very easy to fetch data, but because of that is also separated the returned data and other details from the response inside the response object. That's why you need response.data.status here.

If you use a basis ajax call, you handle the raw response which doesn't have a data key by default. Unless the API returns such a structure of course.

Atef95's avatar

@bobbybouwmann

it really confuses me..

because in case of 422 response which comes automatically with laravel validation...

I have to test on

response.status

otherwise it's response.data.status ...

many if and elses...

bobbybouwmann's avatar

That depends on how you make your controllers. The 422 response is generated by the exception handler in Laravel. The normal responses are probably generated by API-resources, right?

The reason you need to check for response.data is pure axios. I would expect that you would get that on your 4xx responses as well. Unless you give status back yourself.

Can you maybe show an example of your controller with the API-resource?

Atef95's avatar

That's what I'm doing in the controller

    public function handleUpdateSupport(Request $request, $id)
    {
      
        $updateSupport = $this->supports->update($request->all());
        if ($updateSupport) {

            return response()->json(['status' => 200]);
        }
        return response()->json(['status' => 404]);

    }

I would confirm , I think it's pure axios thing as u said

Do u have any idea if is it the same thing happening with that fetch method in react js too ?

it's the same basis ?

Thank you so much for your time :)

Please or to participate in this conversation.