mardi 10 juillet 2018

send json from controller to view with ajax Laravel

I'm trying to send Json from my controller to view via ajax.

Here's the function that generate the Json from the controller :

   public function getBestBank(Request $request)
     {

    $banks = [];
    $bonkas =json_decode('{}');
    $i = 0;
    $Banques = Banque::all();

    //calculer banks
    foreach ($Banques as $banque)
    {

        $total = $banque->CompteCheque + $banque->CarteGold + ( $request->retrait * $banque->RetraitHorsDab ) + ( $request->viremnt*$banque->VirmentMoins10)+( $request->cheque * $banque->cheque)+ ($request->depot*$banque->DepotEspeceAgence)+( $request->montant * $banque->DepotPour50);
        $bonkas->frais = $total;
        $bonkas->name = $banque->name;
        $bonkas->logo = $banque->logoUrl;
        $banks[$i]=$bonkas;
        $bonkas =json_decode('{}');
        $i++;
    }

    $banks = collect($banks)->sortBy('frais');
    $banks = $banks->slice(0, 3);


    return response()->json($banks);
}

and here's the route sending to the function :

  Route::get('/calculer', 'BanqueController@getBestBank');

and here I'm trying to use the Json with Ajax

    function getBanks(emailAdress,retrait,virement,cheque,depot,montant) {

           banks =[];

        $.ajax({
            type: "GET", //rest Type
            dataType: 'json', //mispelled
            url: "/calculer?emailAdress="+emailAdress+"&retrait="+retrait+"&virement="+virement+"&cheque="+cheque+"&depot="+depot+"&montant="+montant,
            async: true,
            contentType: "application/json; charset=utf-8",
            success: function (banks) {

                adaptSelectedBanks(banks);
            }
        });


    }

can you tell me what's wrong ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire