Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

398
Views
Filter product list using Ajax in Laravel

I am implementing product filtering for my project. It works properly in product.main.blade. But when doing it in product.list.blade with the same code structure, the system gives the error "SyntaxError: Unexpected token < in JSON at position 0". As far as I know the cause is the data is not of type JSON. I think the problem lies in the routings causing the data not to be sent. So can anyone help me fix it? Thanks very much!

My Controller:

function ajax_sort(Request $request)
{
    $cat_id = $request->cat_id;
    $select = $request->sort;
    $data=array(
        'cat_id'=>$cat_id,
        'select'=>$select
    );
           
    echo json_encode($data);

    
    
}

Jquery code:

$("form select").change(function() {

    var cat_id = $("#cat-tilte").attr('data-id');
    var sort = $(this).val();
    var data = { cat_id, sort: sort };
    $.ajax({
        url: "ajax_sort",
        method: 'GET',
        data: data,
        dataType: 'json',
        success: function(data) {
            console.log(data);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }

    });
});

And the routes I set up:

Route::get('product/list/{cat_id}',[ProductController::class,'list']);
Route::get('product/main',[ProductController::class,'main']);
Route::get('product/ajax_sort',[ProductController::class,'ajax_sort']);

Edit: The reason is that the URL and Route are incorrect, I have fixed it.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It seems like you have missed this part in your code:

var data = { cat_id:cat_id, sort: sort };
over 4 years ago · Santiago Trujillo Report

0

I found the mistake in the url:

$("form select").change(function() {

    var cat_id = $("#cat-tilte").attr('data-id');
    var sort = $(this).val();
    var url = route('product.ajax_sort', cat_id);
    var data = { sort: sort };
    console.log(url);
    $.ajax({
        url: url,
        method: 'GET',
        data: data,
        dataType: 'json',
        success: function(data) {
      
            $(".result-sort").html(data.result);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }

    });
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!