lavina
2 months ago
202
1
Laravel

Laravel posting data via ajax

Posted 2 months ago by lavina

I am attempting to call my controller that would update my product:

public function update(Request $request, $product_id)
{
    $product = Product::find($product_id);
    $product->name = $request->name;
    $product->test = $request->session()->get('test');;
    $product->save();
    return response()->json($product);
}

I would like to trigger the event with ajax, a button is clicked and my controller is called with the data that then inserts it into database.

I tried to do:

$.ajax({
            type: POST,
            url: my_url,
            data: formData,
            dataType: 'json',
            success: function (data) {
                console.log(data);
            },
            error: function (data) {
                console.log('Error:', data);
            }

But that already posts before calling my controller, how can i just throw all data to controller instead of posting it?

Please sign in or create an account to participate in this conversation.