1
I have gotten an error calling an ajax function contained in a file. js separate from my .html. file The same does not occur when I insert this javascript function into my html file. The function is triggered when I click on a button containing the call to the function, as shown below:
HTML component:
<button type="button" class="btn btn-primary" id="modalAddPessoa" onclick="adicionarPessoa()">Incluir</button>
Javascript function
let requestVerificationToken = $("input[name='__RequestVerificationToken']").val();
function adicionarPessoa() {
$("#incluirPessoas").attr("disabled", true);
var pessoa = new Object();
$.ajax({
type: "POST",
data: pessoa,
url: '/PontoAtendimento/IncluirPessoaPontoAtendimento',
headers: {
"RequestVerificationToken": requestVerificationToken
}
})
.done(function (pessoa, statusText, xhdr) {
$('#incluirPessoas').modal('hide');
$('#tablePessoas').append(pessoa);
})
.fail(function (xhdr, statusText, errorText) {
console.log("Failed: " + errorText);
});
}
When debugging, I got the following error in the browser:
How to solve this problem?
__Requestverificationtoken probably doesn’t exist yet when the js file is being loaded. Try to put this line inside the function.
– Daniel Santos
Apparently that’s right. I put the javascript function inside a $(Document). ready(Function () { }); and the problem was solved.
– Victor Moraes