Good evening Oxbir.
May 3, 2022
22
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
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.
