0

I'm trying to send POST request to a Laravel controller using ajax, BUT nothin happen

Script

<script>
(function($) {
    jQuery('#categorie,#marque').change(function () {
        var id_marq=$('#marque').val();
        var id_cat=$('#categorie').val();
        var url = $('#application_url').val();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url: url+"/produits/ajoutPrixProduit",
            type:"POST",
            data:{'id_cat':id_cat, 'id_marq':id_marq},
            dataType: "json",
            success: function (response) {
                $.each(response, function (key, value) {
                    $('#nom').append('<option value="' + key.id + '">' + value.libelle + '</option>');
                });
            }
        });
    })(jQuery);
});

Route

Route::post('/produits/ajoutPrixProduit', 'MagasinBoardController@selProduit')->name('magasin.selProduit');

Controller

public function selProduit(Request $request)
{
    $id_cat=$request->id_cat;
    $id_marq=$request->id_marq;
   .......
    return response()->json($listProd); 
}

I don't know why and how to fix it

4
  • if you check developer console in client it return which http status code? 200? Commented Mar 21, 2020 at 15:50
  • The http status code is 200 Commented Mar 22, 2020 at 1:33
  • In your controller, if you return $request->all(); what do you get in the dev console? Your jquery is a bit confusing to me. It's probably not wrong. Only written in a style I'm not used to. Commented Mar 23, 2020 at 9:07
  • check this solution stackoverflow.com/a/26413062/13056147 Commented Mar 23, 2020 at 9:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.