When fetching data from a select statement, it is not possible to utilize the 'in' operator to search for the term 'length'

There seems to be an error "Cannot use 'in' operator to search for 'length'" while trying to display data in a table. The URL successfully executes the GET action, but the error occurs when attempting to display the data in the table. Here is the JavaScript code:

<script type="text/javascript>
        $(document).ready(function(){
            $('#kategori').on('change', function(e){
                var id = e.target.value;
                $.get('/khs/khs_semester'+id, function(data){
                    console.log(id);
                    console.log(data);
                    $('#khs').empty();
                    $.each(data, function(index, element){
                        $('#khs').append("<tr><td>"+element.kode_mk+"</td><td>"+element.nama_mk+"</td>"+
                        "<td>"+element.semester+"</td></tr>");
                    });
                });
            });
        });
    </script>
This is my Controller.
public function merkAjax($id){
        if($id==0){
            $terpilih = \DB::table('khs')
                        ->join('matakuliah','matakuliah.kode_mk','=','khs.kode_mk')
                        ->join('dosen','dosen.nidn','=','khs.nidn')
                        ->where('khs.nim',$nim)
                        ->where('khs.semester','1')
                        ->get();
        }else{
        $terpilih =  \DB::table('khs')
                      ->join('matakuliah','matakuliah.kode_mk','=','khs.kode_mk')
                      ->join('dosen','dosen.nidn','=','khs.nidn')
                      ->where('khs.nim',$nim)
                      ->where('khs.semester',$id)
                      ->get();
        }
        return $terpilih;
    }

Answer №1

The in operator is specifically designed for working with objects.

If the data you are sending needs to be parsed, consider converting it to a stringified JSON format.

One way to parse your data is by using JSON.parse(data).

To loop through the parsed JSON data, you can use

$.each(JSON.parse(data), function(index, element){...})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Populate Jquery datatables programmatically

After implementing the Jquery Datatables plugin, I initially had hardcoded content in the table. However, I made some modifications to dynamically populate the table with fetched data. While this change worked fine, I encountered issues with the search f ...

Check to see if the menu item exceeds the allotted space, and if so, display the mobile menu

Currently, I am in the process of creating a unique responsive menu for my website that I think will need some JavaScript implementation. My skills with JavaScript are not very strong, so I am looking for guidance, code examples, or resources that can assi ...

Uncovering the Reasons Behind Multiple Accesses to a ReactJS Page

The code snippet provided shows an issue with accessing the /profile page. Upon accessing the page, there are 6 POST requests sent to the backend. However, after implementing the useEffect hook, while the page is still accessed 6 times, now only 2 POST req ...

Develop a regular expression control specifically designed to validate URLs

I encountered a issue with my current web application. When I access the link: https://localhost:44311/#shop, the page loads perfectly as expected. Here is a screenshot: https://i.sstatic.net/6G6CJ.png However, when I try to change the URL to: https://loc ...

Node.js console and endpoint are returning an object, but the JSON object cannot be fetched

Currently, I am working on a project for an online course where I am utilizing an NLP sentiment analysis API. Although I have made significant progress, I seem to be stuck at the final step. When I send the URL for analysis via the API call, I can see the ...

Accessing a nested JSON array with duplicated attribute names

Is it feasible to access the values in the 'Roofing Insulation' array using JavaScript, especially when dealing with duplicate attribute names? I've attempted various syntaxes like productListing['Roofing Insulation']['Roofin ...

Having trouble with *ngif not functioning properly after initial click in Angular 6

How do I create a Select field in Angular 6 that will hide or display another div based on the selected value? Below is the code snippet: <div class="form-group"> <label for="servicePriviledges">Managed</label> < ...

Tips for sending variables via ajax to the blade template in Laravel

When I select a team from the dropdown menu, I want to display a list of players who play for that team. I have included my code below, but I am unsure how to pass the variable from AJAX directly to Blade and then iterate through it using foreach. Any help ...

Ways to enhance the Response in Opine (Deno framework)

Here is my question: Is there a way to extend the response in Opine (Deno framework) in order to create custom responses? For instance, I would like to have the ability to use: res.success(message) Instead of having to set HTTP codes manually each time ...

Confirming the UTC timestamp in mongoose/Hapijs: A simple guide

Currently, I am developing an application in node.js utilizing Hapi and a MongoDB database with Mongoose. Specifically, I have constructed the subsequent message schema: var schema = { from : { type : Schema.ObjectId, ref : 'User& ...

Error: Unable to locate module: Issue: Unable to find '../components/charts/be.js' in '/vercel/workpath0/my-app/pages'

Having some trouble deploying my next.js app through Vercel. Everything works fine locally with the command 'npm run dev'. But when attempting to deploy it on Vercel using a Github remote repository, I encountered the following error: 18:07:58.29 ...

disabling radio button selection group

I have radio button groups and subgroups that I need to work with. What I am trying to accomplish is to disable the subgroup when the main group's radio button is selected, and then enable it only when the specific subgroup's radio button is chec ...

Divs expanding below content in IE on a particular server

Currently, I am facing an issue with a JavaScript div that expands correctly over other content in most situations but goes under the content in one specific scenario. I am unsure about where to begin debugging this problem. To provide some context, the d ...

Guide on extracting an Array from JSON in a $.ajax response

I received a JSON value that was converted from an array in the AJAX response. {"Text":"Please provide a value","Email":"Please provide a value"} My goal is to extract the response JSON and display it within a div using $(div).html(): Text-Please provid ...

Organize divs sequentially according to their text content using JQuery

Having encountered limitations in the functionality of a particular Wordpress plugin I am utilizing, I find myself faced with the challenge of sorting a customized dropdown in a specific sequence. Is there a way to employ jQuery to target these divs and ar ...

What could be the reason for npm not compiling properly?

I'm currently working with Laravel and Vue.js and recently encountered an error after making a small change in the router.js file. The error occurs during compilation. Any suggestions for a solution? C:\Users\PhpCoder\Documents\G ...

Modifying the key of a JSON file

One of the challenges I'm facing involves handling a JSON file that is being inserted into MongoDB. "entities": [{ "desctype": "Location", "text": "Marília", "relevance": 0.966306, "disambiguation": { "subtype": [ "City ...

Tips for leveraging _.union function from lodash to eliminate duplicate elements from several arrays in a TypeScript project

I attempted to use import _ from 'lodash-es' and _.union(user.users.map(user => user.city)). However, the result was not as expected, such as: ["city_id1", "city_id2", "city_id3", "city_id4"] What is th ...

Can you explain the purpose of App.hiddenDivs in jQuery code snippet provided?

Exploring various JQuery performance tips on this insightful website Do you happen to know the significance of App.hiddenDivs ? ...

Sort table rows by checkbox value in javascript

Is there a way to use javascript or JQuery to group the rows in this table by Office or Age based on the checkbox option selected? For example, if the Office checkbox is checked, the table rows should be grouped by Office. If the Age checkbox is checked, ...