Inertia 请求未收到有效的 Inertia 响应,但收到了纯 JSON 响应。

huangapple go评论53阅读模式
英文:

Inertia requests did not receive a valid Inertia response, however a plain JSON response was received

问题

我正在尝试使用验证器创建错误消息。目前,我已经使用以下代码使其工作:

$validator = Validator::make(request()->all(), [
    'banner' => 'max:2048',
], [
    'banner.max' => 'The :attribute must be less than 2MB.',
]);

if ($validator->fails()) {
    return redirect()
            ->back()
            ->withErrors($validator)
            ->withInput();
}

我不需要在验证失败时重定向用户。因此,我已经删除了重定向响应,但这样做导致我在以下代码更改时收到以下错误。我不确定为什么只收到 JSON 响应。

if ($validator->fails()) {
    return $validator->errors()->all();
}
英文:

I am trying to create an error message using a validator. Currently I got it working it using the following code.

        $validator = Validator::make(request()->all() ,[
            'banner' => 'max:2048',
        ], [
            'banner.max' => 'The :attribute must be less than 2MB.',
        ]);

        if ($validator->fails()) {
            return redirect()
                    ->back()
                    ->withErrors($validator)
                    ->withInput();
        }

I don't need to redirect the user when the validation fails. So I have removed the redirect response however, doing so cause me to receive this error with my following code changes. I am not sure why only JSON response is received.

> All Inertia requests must receive a valid Inertia response, however a
> plain JSON response was received

        if ($validator->fails()) {
            return $validator->errors()->all();
        }

</details>


# 答案1
**得分**: 0

你收到的错误消息表明你返回的是一个普通的JSON响应,而不是一个有效的Inertia响应。Inertia是一个库,允许你使用服务器端路由和客户端渲染构建单页面应用程序。Inertia响应包含在客户端渲染页面所需的数据和视图组件。

要返回有效的Inertia响应,你可以使用Inertia::render方法来渲染带有必要数据的适当视图组件。以下是如何修改你的代码以返回有效的Inertia响应的示例:

```php
use Inertia\Inertia;

if ($validator->fails()) {
    return Inertia::render('YourComponent', [
        'errors' => $validator->errors()->all(),
    ])->toResponse(request()); 
}

在这个示例中,YourComponent是你想要渲染的Inertia组件的名称,errors是一个包含验证错误的数组。toResponse方法用于将Inertia响应转换为可以发送回客户端的标准HTTP响应。

确保将YourComponent替换为你的Inertia组件的实际名称。

英文:

The error message you are receiving indicates that you are returning a plain JSON response instead of a valid Inertia response. Inertia is a library that allows you to build single-page applications using server-side routing and client-side rendering. Inertia responses contain both the data and the view components needed to render the page on the client-side.

To return a valid Inertia response, you can use the Inertia::render method to render the appropriate view component with the necessary data. Here's an example of how you can modify your code to return a valid Inertia response:


if ($validator-&gt;fails()) {
    return Inertia::render(&#39;YourComponent&#39;, [
        &#39;errors&#39; =&gt; $validator-&gt;errors()-&gt;all(),
    ])-&gt;toResponse(request()); 
}

In this example, YourComponent is the name of the Inertia component that you want to render, and errors is an array containing the validation errors. The toResponse method is used to convert the Inertia response to a standard HTTP response that can be sent back to the client.

Be sure to replace YourComponent with the actual name of your Inertia component.

huangapple
  • 本文由 发表于 2023年6月29日 00:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定