0
I tried to call a data engineer based on their service point using ajax, but the result of the ajax is [object Object], where is my mistake?

this is my script

function engineer(url, id, name) {
            // send ajax request to get the cities of the selected province and append to the select tag
            $.ajax({
                url: url,
                type: 'GET',
                data: {
                    id: id
                },
                success: function(data) {
                    $('#' + name).empty();
                    $('#' + name).append('<option>- Choose Option -</option>');
                    $.each(data, function(key, value) {
                        $('#' + name).append('<option value="' + key + '">' + value + '</option>');
                    });
                }
            });
        }
        $(function() {
            $('#servp').on('change', function() {
                engineer('{{ route('engineer') }}', $(this).val(), 'engineer');
            });
        });

and this my controller

return User::all()->where('depart', 6)->where('service_point', $request->id);

and this one is the routes

Route::get('engineer', [TicketController::class,'engineer'])->name('engineer');

I want when selecting a service point an engineer appears who is only at that service point

Gnome
  • 1
  • you might need to do `data = JSON.stringify(data)` in `success: function(data)`, – Sourabh Burse Jan 13 '23 at 08:52
  • https://stackoverflow.com/questions/4750225/what-does-object-object-mean Does this help you? – Juljo Shahini Jan 13 '23 at 08:53
  • Most likely `value` is an object, not a string, so you should use some property to show the correct data you want to show, such as `value.myProperty`. – technophyle Jan 13 '23 at 08:56
  • im tried add your suggest, and reply error like this on console "Cannot use 'in' operator to search for 'length' in" – Gnome Jan 13 '23 at 08:59
  • _"but the result of the ajax is [object Object]"_ is a little weak sentence to be sure with no ambiguity you are saying that: the option elements appended to the target after the ajax response returned, show as `[object, Object]`. If that's the case, the problem lies on `value` as already pointed out. Before doing any assumptions on the value returned by your endpoint, did you try to just console.log(data) inside your success callback? – Diego D Jan 13 '23 at 09:12
  • i've tried add console.log(data) inside success callback, its return information the value is object and im find out to fix that, and im just replace all() in the controller to use pluck, and its solved my problem, it's clear, thanks for helping me – Gnome Jan 13 '23 at 09:45
  • Apologies if I'm telling you something that you already know or have tried. Before executing your AJAX, open the Network tab of the developer console of your browser. Once the AJAX call is completed, click on it, and look at the side pane that opens. In it, click on the Results tab, and you'll see your result there. Also, you could specify beforehand the `dataType` you're expecting. Try going with `dataType: 'json'` in your `$.ajax({...}) setup, and then `console.log(data)` in your `success`; – FiddlingAway Jan 13 '23 at 20:26

0 Answers0