带有axios的POST请求被视为Laravel的GET nuxt

时间:2019-08-15 14:46:45

标签: laravel post request axios nuxt.js

enter image description here我正在向我的laravel后端发出请求。 但是该请求被视为GET而不是POST,我找不到问题所在。

函数如下:

    this.$axios
    .post('/create_car', {
      data: formData
    })
    .then(res => {
      this.status = true
      this.isCreatingCar = false
    })

,并尝试在此控制器功能中接收它:

public function createCar(Request $request) {

    $title = $request->title;
    $previewText = $request->previewText;
    $fuel = $request->fuel;
    $gearbox = $request->gearbox;
    $brand = $request->brand;
    $model = $request->model;
    $year = $request->year;
    $miles = $request->miles;
    $price = $request->price;
    $carType = $request->carType;
    $images = $request->images;

    $car = Car::create([
        'title' => $title,
        'previewText' => $previewText,
        'fuel' => $fuel,
        'gearbox' => $gearbox,
        'brand' => $brand,
        'model' => $model,
        'year' => $year,
        'miles' => $miles,
        'price' => $price,
        'carType' => $carType
    ]);

    // store each image
    foreach($images as $image) {
        $imagePath = Storage::disk('uploads')->put('/cars' . '/' . $car->id, $image);
        carImage::create([
            'carImageCaption' => $title,
            'carImagePath' => 'uploads' . $imagePath,
            'carId' => $car->id
        ]);
    }

    return response()->json(['errors' => false, 'data' => $car]);
}

这是路线:

Route::group(['middleware' => 'throttle:20.5'], function () {
    Route::post('/create_car', 'CarController@createCar');
});

在xamp日志中,它似乎先发送两个请求,然后发送POST,然后相距3秒发送

1 个答案:

答案 0 :(得分:0)

要解决此问题,您需要正确地调用路由,因为api.php中的路由是这样调用的:

this.$axios
.post('/api/create_car', {
  data: formData
})
.then(res => {
  this.status = true
  this.isCreatingCar = false
})