Axios.get returns plain html intead of JSON return data

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Axios Gets Plain HTML Instead of JSON: Debugging API Response Issues in Laravel and React

As developers building modern applications with the Laravel ecosystem and a frontend framework like React, communicating data between the client and server efficiently is paramount. When using axios.get to fetch data from a Laravel API, we expect a clean JSON payload. However, sometimes, what arrives is raw HTML instead. This discrepancy points to a fundamental misunderstanding or misconfiguration in how the server is handling the request, rather than an issue with Axios itself.

This post will diagnose why this happens, examine the provided code snippets, and show you the exact steps needed to ensure your Laravel API consistently returns JSON data to your React application.

The Problem: HTML Instead of JSON

You are attempting to fetch product data using:

javascript
componentDidMount() {
    axios.get('http://localhost:8000/getproducts')
        .then(response => {
            console.log(response.data); // This logs the raw HTML string, not JSON.
        });
}

Your Laravel controller seems correctly set up to return JSON:

php
public function products()
{
   $products = Product::all();
   return response()->json($products); // This *should* return JSON.
}

Yet, the result received by Axios is the entire HTML structure of your application (the <!DOCTYPE html>, <head>, <body> tags), indicating that the server is sending an HTML view instead of data.

Root Cause Analysis: Server Misconfiguration

When a web framework like Laravel receives a request, it determines what to send back based on the return type specified by the controller method. If you are accidentally returning a Blade view instead of a JSON response, the framework renders that view into HTML and sends it as the response body.

The most common reason for this issue is that the route handler is inadvertently pointing to a view file or is configured to render a view directly. While your provided Web.php routes look like standard Laravel routing, the underlying logic in the controller must be strictly adhered to.

When you use methods like return view('some-blade-file'), Laravel automatically handles the rendering and sets the correct Content-Type: text/html header. To force a JSON response, you must explicitly use the JSON response helper.

The Solution: Enforcing JSON Responses in Laravel

The fix lies entirely on the server side—telling Laravel unequivocally that the requested data is meant to be serialized as JSON.

1. Verify the Controller Logic

Ensure your controller method exclusively returns a JSON response using the response()->json() helper. This method automatically sets the appropriate HTTP header (Content-Type: application/json), which is crucial for Axios (and any other API client) to correctly parse the body.

Corrected Laravel Controller Example:

php
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function products()
    {
       $products = Product::all();
       // Explicitly return the data as a JSON response.
       return response()->json($products); 
    }
}

2. Review Route Definitions

Ensure your route definitions are clean and only map to controller actions, not directly to view rendering paths for API endpoints. For pure API fetching, routes should trigger controller logic:

Corrected Web.php Example:

php
use App\Http\Controllers\ProductController;

Route::get('/getproducts', [ProductController::class, 'products']); // Use array syntax for clarity
// Or if using method syntax:
// Route::get('getproducts', [ProductController::class, 'products']); 

By ensuring that every API endpoint explicitly calls response()->json($data), you eliminate the ambiguity between rendering a view and returning raw data. This practice is central to building robust APIs with Laravel, following best practices outlined by resources like those found on the official laravelcompany.com.

Conclusion

The confusion between receiving HTML and expecting JSON stems from a mismatch in how the server constructs its response. In the world of full-stack development, the client (Axios) trusts the server's content type (Content-Type header). If Laravel sends HTML, Axios receives it as text; if it sends JSON with the correct header, Axios parses it correctly into a JavaScript object. Always enforce JSON responses explicitly in your controllers to maintain predictable and reliable API interactions between your React frontend and your Laravel backend.