4
How can I send the user a message that the operation he wants to do is not authorized?
I have in my View a button that calls a Javascript function
<button id="btninicio" onclick="salvaApontamento();">Inicio</button>
That function salvaApontamento()
calls a method in 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 you are the logged in "Administrator":
[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 triggering the request, the return falls straight into the Ajax error and my request does not even arrive in the method there in Controller.
The "Administrator" and the "Operator" see the same View. I would like to send a message to the View that informs the user that their profile is not valid. What parameter I need to capture in my variable data
, there in javascript to know that the method cannot be accessed by the logged in user?
Following the suggestion of friend Eduardo, when I get the statusCode in the ajax javascript, I get the code 200.
When the user is authorized, my data object returns the data I built there in the Controller method.
I believe what Eduardo said is correct.
– PauloHDSousa
When the user is authorized, as is the returned date object?
– EduardoFernandes
Eduardo, I updated the question.
– Emerson
When the result returns with error, there is some field called
result
? Note that when the result returns OK, the value ofresult
is "Success".– EduardoFernandes