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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Uncaught TypeError: Cannot use 'in' operator to search for '324' in a JSON Array

You are encountering this error because of a fundamental mismatch between what your PHP backend is sending and how your JavaScript frontend expects to parse it. The error message Uncaught TypeError: Cannot use 'in' operator to search for '324' in [...] reveals that the code is trying to access an index on a value that isn't an array, or more specifically in this context, the data structure being iterated over has changed unexpectedly during the parsing process.

In your specific case, the root cause lies in how CJSON::encode() handles the data versus how jQuery interprets the response. When you look at your PHP output:

[{"id":50,"name":"SEO"},{"id":22,"name":"LPO"}]

This is a valid JSON array of objects. However, the error suggests that somewhere in the iteration logic, key is being treated as a numeric index (like 324) while value is not behaving like an array element, or the parser is treating the entire response string as a single object rather than an array.

The most likely culprit is the version of jQuery you are using and how it handles JSON parsing in older versions versus modern ones. In very old versions of jQuery (prior to 1.5), the .get() method did not automatically parse the response into a JavaScript object unless you explicitly told it to use dataType: 'json'. If you omit this, jQuery returns the raw string. When you then try to run $.each() on a string, or if the framework you are running inside (Yii) modifies the context, the internal iteration logic can fail with the specific error about using the in operator on the wrong type.

Enforcing JSON Parsing in the Request

The immediate fix is to ensure jQuery explicitly requests JSON data. You need to add the dataType option to your $.get() call. This forces jQuery to parse the response text into a JavaScript object (or array) before passing it to your success callback.

Update your AJAX call as follows:

parentCat.on('change', function(e){
    parentCatId = $(this).val();

    $.get(
        'index.php?r=admin/post/ajax',
        { "parentCatId": parentCatId },
        function(data) {                     
            // Ensure data is an array before iterating
            if (Array.isArray(data)) {
                $.each(data, function(key, value){
                    console.log(key + ":" + value);
                });
            } else {
                console.error("Received non-array data:", typeof data);
            }
        },
        'json' // <--- This is the critical addition
    );
});

By specifying 'json' as the last parameter (or explicitly using dataType: 'json' in newer jQuery syntax), you guarantee that data inside your callback function is a native JavaScript object/array. If the server returns an array, $.each() will iterate over the items correctly.

Handling Legacy Yii JSON Encoding

Looking at your PHP code, you are using CJSON::encode(). This class belongs to older versions of the Yii framework (specifically Yii 1.x). While CJSON usually works well, it is worth noting that modern best practices favor native json_encode() from PHP's standard library or frameworks like Laravel.

If you switch to a more modern stack, such as Laravel, the underlying JSON handling is robust and handles edge cases better than legacy Yii components. In Laravel, you would typically return a response using return response()->json($data);. This ensures that the headers are set correctly (application/json) and that the data is consistently serialized as UTF-8 encoded JSON without the quirks of older C-based JSON extensions.

If you must stay on Yii 1.x, ensure that CJSON::encode is actually returning an array structure and not accidentally wrapping it in a single object string if $catData is somehow formatted differently than expected. However, based on your output log, the raw JSON looks correct for modern parsers; the issue is almost certainly on the client side failing to parse it automatically.

Why the Error Occurs with Numeric Keys

The error message mentions searching for '324'. In JavaScript, when you iterate over an array using $.each, the callback receives a numeric index as the first argument (key). If the data received was actually a string (because of the missing dataType: 'json'), or if the JSON parser failed silently in an older environment, the internal logic checking key in value would throw this specific TypeError because you cannot use the in operator to check for numeric indices on a string.

By enforcing the json data type, you bridge the gap between your PHP backend and your JavaScript frontend, ensuring that key is always a number and value is always an object when iterating over your category list.

Our network