mardi 17 janvier 2017

Laravel 5.2 404 error ajax POST

I'm with a problem in my code, and I can't see it. The code:

Routes

Route::resource("prices", "PriceController", ['except' => ['edit', 'update']]);

Controller

public function store(Request $request)
    {
        if ($request->ajax()) {
            $user = User::findOrFail($request->input("user_id"));
            $user->prices()->save(new Price());
            $price = $user->prices()->latest()->first();
            /*
             * Array's structure ->
             *      price_lines:{
             *          price_line:{
             *              area,
             *              quantity:{
             *                  product,
             *                  qty,
             *                  exempt(if applies the exempt costs),
             *                  factor,
             *                  exempt_factor(if applies the exempt costs),
             *                  cost_expenses:{
             *                      cost_expense:{
             *                          cost,
             *                          expense,
             *                      },
             *                  },
             *                  cost_dealers:{
             *                      cost_dealer:{
             *                          cost,
             *                          dealer,
             *                      },
             *                  },
             *              },
             *          },
             *          price_line:{
             *              area,
             *              quantity:{
             *                  product,
             *                  qty,
             *                  exempt(if applies the exempt costs),
             *                  factor,
             *                  exempt_factor(if applies the exempt costs),
             *                  cost_expenses:{
             *                      cost_expense:{
             *                          cost,
             *                          expense,
             *                      },
             *                  },
             *                  cost_dealers:{
             *                      cost_dealer:{
             *                          cost,
             *                          dealer,
             *                      },
             *                  },
             *              },
             *          },
             *      }
             * */
            foreach ($request->input("price_lines") as $price_line) {
                $area = Areas::findOfFail($price_line['area']);
                $price_line_instance = new PriceLine();
                $price_line_instance->area_id = $area->id;
                $price_line_instance->price_id = $price->id;
                $price_line_instance->save();
                $product = Product::findOrFail($price_line['quantity']['product']);
                $quantity_instance = new Quantity();
                $quantity_instance->product_id = $product->id;
                $quantity_instance->price_line_id = $price_line_instance->id;
                $quantity_instance->qty = $price_line['quantity']['qty'];
                $quantity_instance->exempt = $price_line['quantity']['exempt'];
                $quantity_instance->factor = $price_line['quantity']['factor'];
                $quantity_instance->exempt_factor = $price_line['quantity']['exempt_factor'];
                $quantity_instance->save();
                foreach($price_line['quantity']['cost_expenses'] as $cost_expense)
                {
                    $expense = Expense::findOrFail($cost_expense['expense_id']);
                    $cost_expense_instance = new CostExpense();
                    $cost_expense_instance->expense_id = $expense->id;
                    $cost_expense_instance->quantity_id = $quantity_instance->id;
                    $cost_expense_instance->cost = $cost_expense['cost'];
                    $cost_expense_instance->save();
                }
                foreach($price_line['quantity']['cost_dealers'] as $cost_dealer)
                {
                    $dealer = Dealer::findOrFail($cost_dealer['dealer_id']);
                    $cost_dealer_instance = new CostDealer();
                    $cost_dealer_instance->dealer_id = $dealer->id;
                    $cost_dealer_instance->quantity_id = $quantity_instance->id;
                    $cost_dealer_instance->cost = $cost_dealer['cost'];
                    $cost_dealer_instance->save();
                }
            }
        }
    }

JS

$("#pricing").click(function(event) {
    event.preventDefault();
    var route = $("#route_pricing").val();
    var token = $("#token").val();
    var user = $("#user").val();
    var price_lines = new Array();
    $("#price_body tr").each(function() {
        var area = $(this).data("areaid");
        var qty = $(this).find("td:eq(1)").text();
        var product = $(this).data("productid");
        var dealers_content = $(this).data("dealers");
        var expenses_content = $(this).data("expenses");
        var exempt = $(this).data("exempt");
        var exempt_factor = $(this).data("exemptfactor");
        if (exempt_factor == "") { exempt_factor = 0;}
        var factor = $(this).data("factor");
        var dealers_array = dealers_content.split();
        var expenses_array = expenses_content.split();
        var dealers = new Array();
        dealers_array.forEach(function(item) {
            var values = item.split("/");
            values.forEach(function(item_Q) {
                dealers.push(item_Q);
            });
        });
        var expenses = new Array();
        expenses_array.forEach(function(item) {
            var values = item.split("/");
            values.forEach(function(item_Q) {
                expenses.push(item_Q);
            });
        });
        console.log(dealers.toString());
        console.log(expenses.toString());
        price_lines.push([
            area, [
                product,
                qty,
                exempt,
                factor,
                exempt_factor, [
                    expenses
                ], [
                    dealers
                ]
            ]
        ]);
    });
    var array_string = '{"user":' + user + ',"price_lines":{';
    var count = 0;
    var last = price_lines.length - 1;
    price_lines.forEach(function(item) {
        array_string += '"' + count + '":{"area":' + item[0] + ',"quantity":{';
        array_string += '"product":' + item[1][0] + ',"qty":' + item[1][1] + ',"exempt":' + item[1][2] + ',"factor":' + item[1][3] + ',"exempt_factor":' + item[1][4] + ', "cost_expenses":{';
        var actual = 0;
        var last_of = item[1][5].length - 1;
        item[1][5].forEach(function(item_e) {
            array_string += '"cost_expense":{"cost":' + item_e[1] + ',"expense":' + item_e[0] + '}';
            if (actual < last_of) {
                array_string += ",";
            }
            actual++;
        });
        array_string += '},"cost_dealers":{';
        actual = 0;
        last_of = item[1][6].length - 1;
        item[1][6].forEach(function(item_d) {
            array_string += '"cost_dealer":{"cost":' + item_d[1] + ',"dealer":' + item_d[0] + '}';
            if (actual < last_of) {
                array_string += ",";
            }
            actual++;
        });
        array_string += "}";
        if (count < last) {
            array_string += ",";
        }
        count++;
    });
    array_string += "}}}}";
    console.log(array_string);
    var json = JSON.parse(array_string);
    console.log(JSON.stringify(json));
    console.log(token);
    $.ajax({
        url: "http://localhost:8000/prices",
        headers: {"X-CSRF-TOKEN": token},
        type: "POST",
        dataType: "json",
        data: json,
    }).done(function() {
        swal("ok", "ok", "success");
    }).fail(function(jqXHR, textStatus, thrownError) {
        swal("error", thrownError, "error");
    });
});

The application displays the error when I tried to send the JSON to the route. enter image description here

I was reading, and all that I found was that the most ocurrent error is because of the route, but here the route is correctly craeted, I think.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iElrUN
via IFTTT

Aucun commentaire:

Enregistrer un commentaire