I'm having trouble putting AntiForgeryToken
on my date from json
. Here is the code that I pass the information of my data to controller
without problems.
var todos_servicos = {
ServicoFornecedor,
ServicoDesabilitado: $("#ServicoDesabilitado").val(),
CodigoInterno: $("#CodigoInterno").val(),
Descricao: $("#Descricao").val(),
Observacoes: $("#Observacoes").val()
};
$.ajax({
url: '/ServicosTerceiros/SalvarServicos',
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
cache: false,
data: JSON.stringify(todos_servicos), // passar os parametros
success: function (data) {
To recover the AntiForgeryToken
I did so:
<script type="text/javascript">
function gettoken() {
var token = '@Html.AntiForgeryToken()';
token = $(token).val();
return token;
}
Now when I use the date to send token
it gives me the following error:
"Failed to load resource: the server responded to a status of 500 (Internal Server Error) "
I've made several attempts on my date, follow them:
var jsonString = JSON.stringify(todos_servicos);
$.ajax({
url: '/ServicosTerceiros/SalvarServicos',
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
cache: false,
data: { __RequestVerificationToken: gettoken(), jsonString }, // passar os parametros
success: function (data) {
In my controller
:
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult SalvarServicos(DTO.ServicosTerceiros jsonString)
{
// codigo
}
Declaration in view:
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })