【问题标题】:POST request with axios gets treated as GET nuxt to Laravel使用 axios 的 POST 请求被视为对 Laravel 的 GET nuxt
【发布时间】:2023-03-20 11:56:01
【问题描述】:

我正在向我的 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,然后一个 GET 间隔 3 秒

【问题讨论】:

  • 您能否发布或制作完整错误消息的图像
  • 添加图片
  • 好的,这条路线在哪里,是在 web.php 还是 api.php 中,你是否进一步 100% 确定它是由于调用 create_car 造成的?
  • 我一直在向 api 发送其他请求,它在 api.php 下,几乎 100%,因为我的其他请求正在工作

标签: laravel post request axios nuxt.js


【解决方案1】:

要解决这个问题,你需要正确调用路由,因为api.php 中的路由这样调用它:

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

【讨论】:

  • 实际上我在 nuxt.config 的“baseURL”中有这个。 axios: { baseURL: 'dev.localhost/bil2000-api/public/api' },
  • 你试过这种方式吗?
  • 是的,然后我得到了 404
猜你喜欢
  • 2021-08-02
  • 2020-02-14
  • 1970-01-01
  • 2019-12-20
  • 2013-10-03
  • 2018-04-04
  • 2020-11-03
  • 2020-12-17
  • 1970-01-01
相关资源
最近更新 更多