How can I send the user a message that the operation he wants to do is not allowed?
I have in my View a button that calls a Javascript function
<button id="btninicio" onclick="salvaApontamento();">Inicio</button>
This salvaApontamento()
function calls a method on my Controller:
function salvaApontamento()
{
startSpin();
$.ajax({
type: "POST",
url: getBaseUrl() + "/Apontamentos/AlteraApontamento";,
dataType: "json",
data: JSON.stringify({ apontamentos: jsonDataApontamentos }),
contentType: "application/json; charset=utf-8",
cache: false,
success: function (data) {
stopSpin();
alert("Sucesso);
},
error: function (data) {
stopSpin();
alert("Erro: salvaApontamento() :( " + data.message)
}
});
}
I have a method in my controller that makes changes only if it is the "Administrator" login:
[HttpPost]
[Authorize(Roles = "Administrador")]
public JsonResult AlteraApontamento(ApontamentosOperacao apontamentos)
{
//faz o que eu preciso e retorna um Json...
}
Debugging the code, I realized that if I log in as "Operator". When firing the request, the return goes straight to the Ajax error and my request does not even get into the method there on the Controller.
The "Administrator" and the "Operator" see the same View. I would like to send a message to the View that would inform the user that their profile is not valid. What parameter do I need to capture in my data
variable, there in javascript to know that the method can not be accessed by the logged in user?
Following my friend Eduardo's suggestion, when I get statusCode in ajax javascript, I get the 200 code.
Whentheuserisauthorized,mydataobjectreturnsthedataIbuiltthereintheControllermethod.