Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

jQuery says it cannot use the in operator on a response from PHP

ErrorUncaught TypeError: Cannot use 'in' operator to search for '324' in

Short answer

The in operator only works on objects, and jQuery.each() uses it. The error means your response is still a string, not parsed JSON. Make PHP send a JSON Content-Type and clean JSON with nothing before it, and set dataType: 'json' in the ajax call so jQuery parses the body before the callback.

  • Applies to jQuery 3, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the type of what the success callback receives. If it prints string, the response was not parsed.

    $.ajax({ url: 'data.php', success: function (res) { console.log(typeof res, res); } });
  2. 2Match what you see, then jump to its fix

    1. 1typeof res is string and the value looks like JSONThe response is text and jQuery was never told to parse JSONFix
    2. 2the error callback runs with textStatus parsererror, or the data is a string with HTML before the JSONExtra output makes the JSON invalid, so parsing failsFix
    3. 3the message names a value from your data, and the response is already parsedA primitive is being checked or looped as if it were an objectFix

What does the error look like?

  • Uncaught TypeError: Cannot use 'in' operator to search for '324' in

Cause 1 of 3

The response is text and jQuery was never told to parse JSON

1Check

Without dataType, jQuery guesses from the response MIME type, so a page sent as text/html stays a string. If dataType is given, the Content-Type is ignored.

curl -s -D - -o /dev/null http://localhost/data.php | grep -i content-type

2Fix

Send the JSON header from PHP before any output, and ask for json in the call.

<?php
header('Content-Type: application/json; charset=utf-8');
echo json_encode($rows);

// JavaScript
$.ajax({ url: 'data.php', dataType: 'json' }).done(function (rows) {
  $.each(rows, function (i, row) { /* ... */ });
});

3Verify

typeof res is now object.

console.log(typeof rows);

Confirmed by api.jquery.com

Did this fix it?

Cause 2 of 3

Extra output makes the JSON invalid, so parsing fails

1Check

jQuery parses JSON strictly and throws on malformed input, and since 1.9 an empty body is rejected too. Whitespace before the opening <?php tag, a PHP warning, or a stray echo counts as output. Look at the raw response.

curl -s http://localhost/data.php | head -c 300

2Fix

Remove everything that prints before the JSON, and for an empty result return null or an empty object or array instead of nothing.

<?php
header('Content-Type: application/json; charset=utf-8');
echo json_encode($rows ?? []);

3Verify

The raw response starts with [ or { and the error callback no longer runs.

curl -s http://localhost/data.php | head -c 50

Confirmed by api.jquery.com

Did this fix it?

Cause 3 of 3

A primitive is being checked or looped as if it were an object

1Check

The in operator throws a TypeError when the right-hand side is a primitive such as a string. This also happens when a single value is returned where code expects a list.

console.log(Array.isArray(rows), typeof rows);

2Fix

Return an array or object from PHP in all cases, or guard in the script before looping.

if (rows && typeof rows === 'object') {
  $.each(rows, function (i, row) { /* ... */ });
}

3Verify

The loop runs for lists and is skipped for a plain string.

console.log(typeof rows);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Always send Content-Type: application/json from endpoints that return JSON, and set dataType on the client.
  • Keep PHP warnings out of JSON responses, so output is never mixed with the body.
  • Return an empty array, not an empty body, when there is no data.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.