Home > Mobile >  Uncaught SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
Uncaught SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

Time:06-03

I have been reading some of the other questions here but I can't find one that fully explains how I should fix it. I am new to JSON and don't fully understand it all.

I have to search price range(min_price & max_price) from two columns(regular_price & sale_price) but unable to get values from both columns.

<script>
    $( function() {
        $( "#slider-range" ).slider({
            range: true,
            min: {{ $min_price }},
            max: {{ $max_price }},
            values: [ 0, {{ $max_price }} ],
            slide: function( event, ui ) {
                var amount = $( "#amount" ).val( " $ "   ui.values[ 0 ]   " -  $ "   ui.values[ 1 ] );
                $.ajax({
                    type: 'get',
                    dataType: 'json',
                    url: '{{ route('priceFilter') }}',
                    data: 'amount',
                    success: function (response) {
                        var data = JSON.parse(response);
                        $.each(data, function (index, element) {
                            console.log(element);
                        });
                    }
                });
            }
        });
        $( "#amount" ).val( " $ "   $( "#slider-range" ).slider( "values", 0 )   " -  $ "   $( "#slider-range" ).slider( "values", 1 ) );
    });
</script>

I click on the range I get the following error:

Uncaught SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

CodePudding user response:

that was your line:

data: 'amount',

you should may change it to this format:

 $.ajax({
         url: 'something.php',
         data: {amount: amount},
                type: 'POST',
                success: function (data) {
                    if (!data.error) {
                       //your code
                    }
                }
            });

CodePudding user response:

As in the answer to Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data on jQuery, and as said in the comment:

with dataType: "json", the json.parse isn't necessary.

Change

                    success: function (response) {
                        var data = JSON.parse(response);
                        $.each(data, function (index, element) {
                            console.log(element);
                        });
                    }

to

                    success: function (response) {
                        $.each(response, function (index, element) {
                            console.log(element);
                        });
                    }
  • Related