Laravel 5.2 404错误ajax POST

时间:2017-01-17 19:48:23

标签: php laravel-5 laravel-5.2

我的代码中存在问题,我看不到它。代码:

路线

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

控制器

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

当我尝试将JSON发送到路由时,应用程序显示错误。 Error in the console

我正在阅读,而我发现的所有事情都是由于路线造成的最偶然的错误,但我认为这里的路线是正确的。

1 个答案:

答案 0 :(得分:0)

您正在发帖user

var array_string = '{"user":' + user + ', ...

但是在寻找user_id

$user = User::findOrFail($request->input("user_id"));

由于找不到匹配的用户,findOrFail throws a 404