从控制器发送json以使用ajax Laravel查看

时间:2018-07-10 18:55:51

标签: json ajax laravel-5 model-view-controller

我正在尝试从控制器发送Json以便通过Ajax查看。

这是从控制器生成Json的函数:

   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);
}

这是发送到函数的路由:

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

在这里,我正在尝试将Json与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);
            }
        });


    }

你能告诉我怎么了吗?

0 个答案:

没有答案