Jc John
Jc John

Reputation: 1859

using ajax in laravel 5.4 ERROR : Not found

why is this i have this error not found in my Network Console from my web browser?

Here is what I've done :

My ajax:

 function getProducts(category_id) {
    $("#product-list").empty();
    $.ajax({
        url:'{{URL::to('home.products')}}/"+ category_id',
        type:"GET",
        dataType: "JSON",
            success: function(data) {

            }
    });
}

My Route:

Route::get('/home/products', 'HomeController@productsbyCat')->name('home.products');

My Controller:

public function productsbyCat($category_id) 
    {
        $products = DB::table('products')
        ->select('products.product_id','products.featured_img','products.product_name','products.description',
                 'products.price','products.quantity')
        ->join('products', 'categories.category_id', '=', 'products.category_id')
        ->where('products.status', 'published')
        ->where('products.category_id', $category_id)
        ->get();

        return $products;
    }

please help me check my code if there are need to be change? or am i returning already an JSON FILE FORMAT in return $products ?

Upvotes: 0

Views: 73

Answers (1)

R.S Namdhari
R.S Namdhari

Reputation: 93

Try this in your route

Route::get('/home/products/{category_id}', 'HomeController@productsbyCat')->name('home.products');

and in ajax request try url like this

url:'{{URL::to("home.products")}}/'+ category_id',

Upvotes: 1

Related Questions