I am working on a website where I need to implement a button that checks if the user has specific permissions before opening a new web page in a popup window. In my JavaScript code, I have the following function:
function sendAjax(methodName, dataArray, success, error, category) {
var error2 = error || function () { };
$.ajax({
type: 'POST',
url: '/PermissionChecker' + methodName,
data: JSON.stringify(dataArray),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (r, s, x) {
if (!success || success(r, s, x) != false) {
if (typeof (window.ChangeLogAdd) == 'function') {
ChangeLogAdd(category);
}
}
},
error: error2
});
}
function CheckPermissions() {
sendAjax("CheckPermission", null, function (data) {
var permission = eval('(' + data + ')');
if (permission == true) {
alert('yay');
} else {
alert('nay');
}
}, null, 'Check Permission');
}
On the C# side, there is a simple function that performs a check and returns a boolean value. The function call works correctly, but when it returns the boolean value, I encounter a JavaScript error stating "Expected ']'." This seems to be related to the success function in my code:
function (data) {
var permission = eval('(' + data + ')');
if (permission == true) {
alert('yay');
} else {
alert('nay');
}
}
I did not face this error before implementing the mentioned block of code, which makes me wonder about the correct way to retrieve the data from the AJAX call and pass it for the permission == true
conditional check.