Axios response.data return HTML instead of an object

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Decoding the Error: Why Axios Returns HTML Instead of JSON from Your Laravel API

As a senior developer working with modern full-stack frameworks like Laravel and Vue.js, we frequently encounter frustrating issues when building APIs. One of the most common stumbling blocks is receiving unexpected data formats—specifically, getting raw HTML content back from an endpoint that is supposed to return structured JSON.

This post dives deep into the scenario you described: using Axios to fetch data from a Laravel API, only to find that response.data contains an HTML string instead of a usable JavaScript object. We will diagnose the root cause and implement the best practices to ensure your backend serves clean, predictable JSON responses.

The Problem: Content Negotiation Failure

When you use Axios to make a GET request, it expects the server to respond with data in a machine-readable format, typically JSON (application/json). If the server mistakenly sends back an HTML document (like a redirect or an error page) instead of JSON, Axios dutifully returns that raw HTML string in the response.data field.

In your specific setup, where you are using Laravel with Passport authentication and Vue with Axios, this usually points to one primary cause: the controller method is returning a Blade view instead of a JSON response.

When Laravel executes a route handler without explicitly telling the framework to serialize data as JSON, it defaults to rendering a view. If that view is returned directly, or if some middleware forces an HTML output before Axios can intercept the stream, you end up with HTML noise in your frontend data.

Diagnosing the Backend: The Controller's Role

Let's look at the critical piece of code where this discrepancy occurs: your DocumentController@list method and its interaction with the view system.

If your controller method attempts to return a full Blade view (e.g., by implicitly calling a view function or returning a string that Laravel treats as a view), the resulting output stream will be HTML, not JSON.

The Incorrect Scenario (What is likely happening)

If your DocumentController@list method inadvertently returns something that triggers a view rendering process instead of JSON serialization:

// Example of what might lead to HTML being returned if not handled correctly
public function list(Request $request) {
    // If this somehow results in returning a view, the body will be HTML
    return view('documents'); 
}

When Axios receives this response, it sees the content type as text/html and places the entire HTML source into response.data.

The Solution: Enforcing JSON Serialization

The fundamental solution lies in explicitly instructing Laravel to serialize your data into a JSON response format using the response()->json() helper. This ensures that the HTTP response header correctly signals to Axios that it is receiving structured data, and the body contains valid JSON objects.

Correct Implementation in the Controller

You must ensure that every API endpoint returns a structured PHP array or Eloquent collection wrapped in a JSON response.

// app/http/Controllers/DocumentController.php

use Illuminate\Http\Request;
use App\Models\Document; // Assuming you are using Eloquent models

public function list(Request $request)
{
    // 1. Retrieve the data using Eloquent (a core part of Laravel development)
    $documents = Document::all(); 
    
    // 2. Explicitly return the data as a JSON response
    return response()->json($documents);
}

By using response()->json($documents), you are telling the framework: "Take this PHP data, convert it into a JSON string, set the Content-Type header to application/json, and send it back." This adheres strictly to RESTful API principles, which is fundamental to building robust services on platforms like Laravel.

Summary and Best Practices

The issue you faced is not a flaw in Axios or Vue; it is a mismatch in how your Laravel backend was configured to handle API requests versus view requests.

  1. Always Return JSON: The golden rule for any API endpoint in Laravel is to use return response()->json([...]) when dealing with data retrieval.
  2. Separate Concerns: Keep your presentation logic (Blade views) separate from your data delivery logic (API controllers). Controllers should focus purely on fetching and formatting data, not rendering HTML.
  3. Use Laravel Features: Leverage Laravel’s built-in response helpers to simplify the serialization process, making your APIs cleaner and more reliable.

By correctly implementing JSON responses in your API layer, you ensure that your frontend (Vue/Axios) receives exactly what it expects—clean data ready for display—leading to a much smoother development experience when building complex SPAs on top of Laravel services.