如果您正在使用this.$inertia
,它会等待Inertia响应;
this.$inertia.get(route('example'))
.then(res => {
console.log(res)
})
请使用 axios
替代
axios.get(route('example'))
.then(res => {
console.log(res)
})
虽然略晚回复,但在您的控制器中:
return Redirect::back()->with([
'data' => 'Something you want to pass to front-end',
])
然后在前端:
this.form.post(route(...), {
onSuccess: (res) => {
this.form.data = res.props.data
},
})
this.form
在 data(){ ...
中被设置为以下内容。form: this.$inertia.form({
_method: 'PUT',
}),
(根据您的需求调整)
在通过惯性成功更新表单后,data
存在于 props
响应中。这个答案帮助了我,当时我正在寻找一个答案。
这个答案 帮助了我到达这里,虽然不完全相同。希望我的答案有所帮助!
->with()
将返回的数据存储在用户会话中。如果你返回了大量数据,这可能会导致问题。例如,在使用 database
会话驱动程序时,会导致 sessions
表上的 payload
列过大。 - DrowsyisInertiaResponse(response) {
return response?.headers['x-inertia']
}
如果您使用Inertia::render,响应头中已经包含了此信息:
X-Inertia: true
仅有浏览器未将此标题提供给JavaScript,这是出于安全原因由您的浏览器执行的。
您可以尝试将此添加到您的 config/cors.php 中:
'exposed_headers' => ['x-inertia']
如果您使用浏览器的网络检查工具,您会在响应中看到一个添加的标头:
Access-Control-Expose-Headers: x-inertia
基于这个头,浏览器会将“X-Inertia”头信息提供给JavaScript(然后弹出窗口将消失)。
考虑到CORS是一种安全措施,以这种方式添加内容可能会带来安全风险,特别是在使用通配符而不是定义值时更是如此。为了使这个例子完整并运作良好,config/cors.php还需要做出相应修改:
'allowed_origins' => ['your-frontend.domain'],
'paths' => [ '/path-you-are-requesting' ],
'allowed_methods' => [ 'GET' ]
'allowed_headers' => [ 'content-type,x-inertia,x-inertia-version,x-requested-with' ]
axios.get("http://example.com",).then((res) => {
console.log(res.data)
})