I'm using Laravel (as api) with Sanctum to Authentication and for client side NextJs 13 with App router.
我将Laravel(作为API)与Sanctom一起使用进行身份验证,并将其用于客户端NextJs 13与App路由器。
I want to use in Next Static Site Generation so I want to call an laravel endpoit in a page like this:
我想在下一代静态站点中使用,所以我想在这样的页面中调用laravel endpoit:
import CategoryService from "services/CategoryService";
import { CategoryView } from "views/admin/category";
import { XSRF_TOKEN } from "utils/cookies/constants";
import { cookies } from 'next/headers'
export default async function Page() {
const fds = cookies().get('final_draw_session')?.value;
const xsrf = cookies().get(XSRF_TOKEN)?.value
const response = await CategoryService.getAll(fds, xsrf);
const categories = response.data.data;
return (
<CategoryView categories={categories} />
)
}
and in the CategoryService I have:
在CategoryService中,我有:
class CategoryService {
async getAll(fds, xsrf) {
return await httpAPIClient.get('/categories', {
headers: {
'XSRF-TOKEN': xsrf,
'final_draw_session': fds
}
})
}
}
export default new CategoryService();
If I inspect the console we can see that the headers were set:
如果我检查控制台,我们可以看到标头已设置:
But I got an error
但我弄错了
In postman this works
在《邮递员》中,这一点是可行的
I need help to call some protected laravel endpoints in Server Side in NextJs because I also tried using axios interceptors to set the cookie but I can't get the client cookies in Server side
我需要帮助来调用NextJs中服务器端的一些受保护的laravel端点,因为我也尝试使用axios拦截器来设置Cookie,但我无法在服务器端获得客户端Cookie
更多回答
If you are getting a 500, just check the laravel logs, it will be there, or check the browser network tab for the response (content) so you can see the error
如果您得到的是500,只需检查laravel日志,它就会在那里,或者检查浏览器网络选项卡中的响应(内容),这样您就可以看到错误
优秀答案推荐
我是一名优秀的程序员,十分优秀!