jQuery says it cannot use the in operator on a response from PHP
Uncaught TypeError: Cannot use 'in' operator to search for '324' inShort 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.
Fix it fast
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); } });2Match what you see, then jump to its fix
- 1
typeof res is string and the value looks like JSONThe response is text and jQuery was never told to parse JSONFix - 2
the 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
the 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
- 1
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-type2Fix
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
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 3002Fix
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 50Confirmed by api.jquery.com
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
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
Back end
json_decode() gives NULL for JSON that looks valid
Syntax error
json_decode returns null when it cannot decode the input, and also when the JSON really is null.
4 causes, each with a fixWeb apps
CodeIgniter 3 shows 'The action you have requested is not allowed.'
403 Forbidden
With csrf_protection on, CodeIgniter 3 compares the POST token with the CSRF cookie and returns this 403 when either is missing or they differ.
4 causes, each with a fixWeb apps
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
The text your script returned is not JSON.
4 causes, each with a fixWeb apps
WordPress media upload fails with "Unexpected response from the server"
413 Request Entity Too Large
WordPress shows this message when the upload request did not come back as the JSON it expects.
3 causes, each with a fixWeb 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 fixWeb apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fix