gpt4 book ai didi

javascript - 防伪 token Axios Asp .Net Core

转载 作者:行者123 更新时间:2023-11-30 19:35:19 32 4
gpt4 key购买 nike

您好,我可以像这样使用 vue js axios 将帖子发送到 Asp .NET Core 2.2 中的 Controller

                axios({
url: '/Parametros/Create',
method: 'post',
ContentType: 'application/json',
data: formData
})

但是为了让它工作,我必须从 Controller 中的操作中删除

[ValidateAntiForgeryToken]

token 也在 razor 页面中作为输入生成

<input name="__RequestVerificationToken" type="hidden" value="CfDJ8GwWLSmGzLVOqfs-yISjocyQshOjT98BeCqxo14sO91JGUZPe_IstyK9DWZyu0rCr0bxdx3lBlwminvxm7q0zXVWcUkAZIH8NwKDYGdNCiY-Z_BgMzLt_1PyNEHxfpmTouJgMu3il8N4fMjbI0ohwElXGK-eVLXGuzj_J5N_uQ3A4f-9ijmTKGk8p3BC7hrB1A">

我试过了

axios({
url: '/Parametros/Create',
method: 'post',
headers: {
"__RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val();
}
ContentType: 'application/json',
data: formData
})

axios({
url: '/Parametros/Create',
method: 'post',
ContentType: 'application/json',
data: {
"__RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val(),
formData
}
})

没有用,我不断收到错误的请求...使用 ajax,第二种方法工作正常,但 axios 不行。我该如何处理?

最佳答案

将 header 中的 __RequestVerificationToken 更改为 RequestVerificationToken 使发布请求对我有用。

最终代码:

         axios({
method: 'post',
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val()},
url: 'home/axiostest',
})
...

关于javascript - 防伪 token Axios Asp .Net Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56006103/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com