我没有使用Axios收回错误

发布于 2025-02-10 21:45:39 字数 1062 浏览 0 评论 0原文

即使使用Axios使用了Validate()方法,我也不会遇到错误。

这是我的控制器,

public function store(Request $request)
{
      $fields = $request->validate([
         'product_name' => 'required',
        'product_cost' => 'required | integer',
        'product_selling' => 'required | integer',
        'product_stock' => 'required | integer',
 ]);

Product::create([
    'name' => $fields['product_name'],
    'cost' => $fields['product_cost'],
    'selling' => $fields['product_selling'],
    'stock' => $fields['product_stock'],
]); }

这是我的vue文件,

    const productState = reactive({
        product_name: "",
        product_cost: "",
        product_markup: "",
        markup_type: "Markup by price",
        product_selling: "",
        product_stock: "",
        returned_errors: [],
    });

   axios .post("api/products", productState)
    .then((response) => {
        console.log(response);
    })
    .catch((error) => console.log(error.response.data.errors));

即使它有错误仍然返回响应。

I'm not getting errors even though I have used validate() method using axios.

This is my controller,

public function store(Request $request)
{
      $fields = $request->validate([
         'product_name' => 'required',
        'product_cost' => 'required | integer',
        'product_selling' => 'required | integer',
        'product_stock' => 'required | integer',
 ]);

Product::create([
    'name' => $fields['product_name'],
    'cost' => $fields['product_cost'],
    'selling' => $fields['product_selling'],
    'stock' => $fields['product_stock'],
]); }

This is my Vue file

    const productState = reactive({
        product_name: "",
        product_cost: "",
        product_markup: "",
        markup_type: "Markup by price",
        product_selling: "",
        product_stock: "",
        returned_errors: [],
    });

   axios .post("api/products", productState)
    .then((response) => {
        console.log(response);
    })
    .catch((error) => console.log(error.response.data.errors));

Even if it has errors still it returns the response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏花。依旧 2025-02-17 21:45:39
   try {
        $fields = $request->validate([
            'product_name' => 'required',
            'product_cost' => 'required | integer',
            'product_selling' => 'required | integer',
            'product_stock' => 'required | integer',]);
    } catch (ValidationException $ex) {
        return response()->json(['errors' =>$ex->errors()], 422); //what ever error format that you desire
    }

您应该尝试catch $ request-> validate,如果您发送API请求,请将自定义响应发送回。

但是我不建议您使用$ request-> validate API请求您可以选择表单请求或valifator :: make(),以便为API请求提供更多灵活性,并且另外,您也可以在handler.php文件中查看验证异常并在此处处理。

已经发布了很多文章,因此请尝试研究“针对API请求的Larvel验证”。
(我不会链接到您可以在博客文章中找到YouTube视频中的特定文章),

它将帮助您了解整个Laravel验证过程。

   try {
        $fields = $request->validate([
            'product_name' => 'required',
            'product_cost' => 'required | integer',
            'product_selling' => 'required | integer',
            'product_stock' => 'required | integer',]);
    } catch (ValidationException $ex) {
        return response()->json(['errors' =>$ex->errors()], 422); //what ever error format that you desire
    }

You should try catch the $request->validate and send the custom response back, if you are sending API request.

But I don't suggest using $request->validateAPI requests you can go for Form Request or Validator::make() for more flexibility for api requests too and also you can catch the validation exception in the Handler.php file too and handle it there.

There are lots of articles already posted so try to research "Larvel Validation for API requests".
(I am not gonna link to the specific articles you can find Youtube videos Medium articles to blog posts)

It will help you to understand the Laravel validation process as a whole.

魂归处 2025-02-17 21:45:39

您是否尝试过:

error.response.data.error

或只是记录错误并查看结构是什么

Did you try with:

error.response.data.error

or just log error and see what is the structure

小姐丶请自重 2025-02-17 21:45:39

Axios中的返回错误并不总是具有error.Response属性。您可能应该将错误处理构建为在AXIOS文档中的此示例中。

希望这会有所帮助。

The returned error in Axios not always has the error.response property. You probably should structure your error handling as in this example in the Axios docs.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文