Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Laravel:SyntaxError: "[object Object]" is not valid JSON [Solved]

javascript-unexpected-token-o-in-json-at-position-1

update ho gaya successfully
VM1415:1 Uncaught SyntaxError: "[object Object]" is not valid JSON
    at JSON.parse (<anonymous>)
    at Object.success (websiteaccess:1706:40)
    at c (vendor.bundle.base.js:11757:56)
    at Object.fireWith [as resolveWith] (vendor.bundle.base.js:11794:96)
    at l (vendor.bundle.base.js:13594:415)
    at XMLHttpRequest.<anonymous> (vendor.bundle.base.js:13682:235)
Enter fullscreen mode Exit fullscreen mode

Problem

 var mydata = JSON.parse(data)
Enter fullscreen mode Exit fullscreen mode
 success: function(data) {
                        console.log('update ho gaya successfully');


                        // adding alert messages                
                     var mydata = JSON.parse(data)
                       console.log(data);
                        // adding alert messages for success and exist data validation open
                        var html = '';
                        if (data.success) {
                            html = '<div class="alert alert-success">' + data.message + '</div>';
                            $('#form_result').html(html);
                            setTimeout(function() {
                                $('#formModal').modal('hide');
                                $('#WebAccessTable').DataTable().ajax.reload();
                            }, 2000);
                        } else {
                            html = '<div class="alert alert-danger">' + data.message + '</div>';
                            $('#form_result').html(html);
                        }
                        // adding alert messages for success and exist data validation close
                    },
Enter fullscreen mode Exit fullscreen mode

Solution

   const parsed = JSON.parse(JSON.stringify({data}));               

                   console.log(parsed);
Enter fullscreen mode Exit fullscreen mode
 success: function(data) {
                        console.log('update ho gaya successfully');


                        // adding alert messages                
                        const parsed = JSON.parse(JSON.stringify({data}));               

                   console.log(parsed);
                        // adding alert messages for success and exist data validation open
                        var html = '';
                        if (data.success) {
                            html = '<div class="alert alert-success">' + data.message + '</div>';
                            $('#form_result').html(html);
                            setTimeout(function() {
                                $('#formModal').modal('hide');
                                $('#WebAccessTable').DataTable().ajax.reload();
                            }, 2000);
                        } else {
                            html = '<div class="alert alert-danger">' + data.message + '</div>';
                            $('#form_result').html(html);
                        }
                        // adding alert messages for success and exist data validation close
                    },
                    // message alert close
                    error: function(data) {
                        console.log('Error:', data);
                        //    this function for hide with id #formModel
                        console.log('update function kamm nahi kr rha hai');
                    }
                });
Enter fullscreen mode Exit fullscreen mode

=========================================================

in laravel

public function getData($id)
    {       
       log::info("country getdata hain");
        $datas = DB::table('countries')->where('id',$id)->first();  
        $query=$datas->currency;
        log::info($query);
        $data = [
            'website' => $query
        ];

         return response()->json($data);   
    }
Enter fullscreen mode Exit fullscreen mode

in html

<form method="POST" action="/registers">
    @csrf


    <label for="email">Email</label>
    <select  id="myDropdown" name="country">
    <option value="">Select a country</option>
    @foreach ($countries as $country)
        <option value="{{ $country->id }}">{{ $country->country_name  }}</option>
    @endforeach
</select>

<label for="username">currency</label>
    <input id="username" type="text" name="username" required>

    <button type="submit">Register</button>
</form>
Enter fullscreen mode Exit fullscreen mode

In jquery

$(document).ready(function() {

  var dropdown = $('#myDropdown');

// Apply the condition after selecting an option
            dropdown.change(function() {
        // Get the selected option value
        var selectedOption = dropdown.val();
        var id = $(this).val();
        // Apply the condition based on the selected option

            $.ajax({
                url: '/get-data/' + id,
                type: 'GET',
                dataType: 'json',               
                success: function(data) {
                  console.log("data are here");
                  const parsed = JSON.parse(JSON.stringify({data}));               
               console.log(parsed);
               $('#username').val(data.website);


                }
            });

});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)