Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

JSON.parse: unexpected end of data at

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.

HTML

<div class="card mb-3">
    <div class="card-body">
        <p>
            <label for="amount">amount:</label>
            <input type="text" name="amount" id="amount" readonly class="border-0 fw-bold text-warning">
        </p>
        <div id="slider-range"></div>
    </div>
</div>

SCRIPT

<script src="{{ asset('themes/js/jquery-ui.js') }}"></script>
<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: 'POST',
                    dataType: 'json',
                    url: '{{ route('priceFilter') }}',
                    data: {amount: amount.val()},
                    success: function (data) {
                        if (!data.error) {
                            var data = JSON.parse(data);
                            $.each(data, function (index, element) {
                                console.log(element);
                            });
                        }
                    }
                });
            }
        });
        $( "#amount" ).val( " $ " + $( "#slider-range" ).slider( "values", 0 ) + " -  $ " + $( "#slider-range" ).slider( "values", 1 ) );
    });
</script>

Controller

public function priceFilter(Category $category, Request $request)
{
    $categories = Category::all();
    $colors = Color::all();
    $brands  = Brand::all();
    $min_price = Product::min('price');
    $max_price = Product::max('price');
    $filter_min_price = $request->min_price;
    $filter_max_price = $request->max_price;
    $range = [$filter_min_price, $filter_max_price];
    $products = Product::query()->whereBetween('price', $range)->get();

    if($filter_min_price && $filter_max_price){
        if($filter_min_price > 0 && $filter_max_price > 0)
        {
            $products = Product::all()->whereBetween('price', [$filter_min_price, $filter_max_price]);
        }
    } else {
        $products = Product::all();
    }
    return response()->json($category);
    //return view('Home.contents.category',compact('products','categories','min_price','max_price','filter_min_price','filter_max_price', 'category', 'colors', 'brands'));
}

I see this error.

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

0 likes
22 replies
Snapey's avatar

You get $categories

Then you get products (twIce)

Then you just return a json encoded version of the category model

You have tested this function haven't you? You have looked in the browser network tool haven't you? YOU HAVE LOOKED IN THE LARAVEL LOGS HAVEN'T YOU ??????

Snapey's avatar

@qhom I have a desk that I sit at every day, thanks. Why? Are you giving up programming and going into office furniture? That would be a wise career move.

Snapey's avatar

@qhom did you cut pieces of code from 5 different projects and put them on one file?

saifqureshi341's avatar
Level 2

You're returning $category variable which even doesn't exist in your code, I think want to return $products

public function priceFilter(Category $category, Request $request)
{
    $categories = Category::all();
    $colors = Color::all();
    $brands  = Brand::all();
    $min_price = Product::min('price');
    $max_price = Product::max('price');
    $filter_min_price = $request->min_price;
    $filter_max_price = $request->max_price;
    $range = [$filter_min_price, $filter_max_price];
    $products = Product::query()->whereBetween('price', $range)->get();

    if($filter_min_price && $filter_max_price){
        if($filter_min_price > 0 && $filter_max_price > 0)
        {
            $products = Product::all()->whereBetween('price', [$filter_min_price, $filter_max_price]);
        }
    } else {
        $products = Product::all();
    }
    return response()->json($products);
}
1 like

Please or to participate in this conversation.