How to tell whether AJAX give JSON response not a html page in php?

Dani AI

Generated

A compact, practical checklist for confirming whether an AJAX request actually returned JSON or an HTML page. Common reasons a response looks like HTML: PHP warnings/notices or debugging tool output injected into the body, an automatic redirect to a login/error page, accidental whitespace or a BOM before/after PHP tags, or a server error page. ’s question got useful pointers from (tell the client to expect JSON) and (watch cross‑domain/jsonp); below are concrete diagnostics and fixes to find where the HTML is coming from.

Client-side checks: inspect the request in the browser DevTools → Network. Confirm the response headers include a JSON content type and inspect the raw response body for leading “<” or an HTML doctype. Run a raw request from the command line to see headers and body exactly as sent, and guard JSON parsing with error handling so parsing failures are caught early.

curl -i 'https://example.com/api/endpoint'

fetch('/api/endpoint', { method: 'POST' })
  .then(res => {
    const ct = res.headers.get('content-type') || '';
    if (!ct.includes('application/json')) throw new Error('response not JSON: ' + ct);
    return res.json();
  })
  .catch(err => console.error(err));

Server-side checks: make sure no output is emitted before the JSON headers (no stray whitespace or BOM, avoid an unnecessary closing PHP tag), disable display_errors in production so notices/warnings don’t turn into HTML, and send an explicit JSON content-type header before any output.

header('Content-Type: application/json; charset=utf-8');

Also validate the JSON on the server (use the language’s JSON error reporting functions), check server logs for warnings, and verify the request isn’t being redirected (session/login redirects commonly return HTML). For cross‑domain calls, confirm CORS headers are present or use the correct JSONP/CORS flow. Quick checklist: DevTools or curl → check headers and raw body → validate parsing → inspect server logs → remove stray output.

Recommended Answers

All 2 Replies

What do you mean? Can you post code?

if you want ajax to send json you need to tell ajax function to send json, if you want php to send json you tell php json_encode(); or you can use json_decode();

with jquery its pretty easy.

var data = 'object':{'key'=>'value', 'key2' => 'value2'}; // Create json object

$.ajax({
url: '', // url to php script
type: "POST", //Send post method to php
dataType: "json"; // return json from php script
data: data, // Post data
success: function(data){
console.log(data);
}

});
Member Avatar for Member #120589

Well I would imagine that if you're doing an ajax call on your own pages, you'd know anyway? Are you doing x-domain calls e.g. with jsonp?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.