AJAX POST 403 权限被拒绝错误 - 即使我正在发送身份验证 headers
AJAX POST 403 Permission Denied Error - Even though I am sending auth headers
我在尝试 POST 到我的 API 时遇到了这个问题。它 returns 出现以下错误:
Uncaught Error.
{"meta":{"httpCode":403,"message":"Permission denied."},"data":[]}
我发送的授权适用于填充我页面上数据表的 AJAX GET。我想知道是不是我的数据格式有误之类的。 POST 所需的数据只是名称和描述,所以我认为我不会遗漏任何内容。谁能看出此代码中可能导致 403 错误的任何错误?
var name = $('#name').text();
var description = $('#description').text();
$('#add-category').on('click', function() {
$.ajax({
type: "POST",
url: baseUrl + "api/categories",
headers: {
"Authorization": auth
},
data: {
"name": name,
"description": description
},
dataType: 'json',
contentType: 'application/json',
error: function(jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connected.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg);
},
success: function() {
table.draw();
}
});
});
感谢查看!
编辑:这是 API 中的相关函数,我没有看到任何关于 403 错误的信息。也许我应该看看 API?
的其他地方
public function categories_post()
{
$this->requirePermission('categories.create');
if (!$postData = $this->post(null, false)) {
$this->errorResponse('Request missing category data in request body.', 405);
}
// @TODO Better Validation.
$postData = $this->xssClean($postData);
$category = $this->gameService->createCategory($postData);
$categoryData = $this->categoryHydrator->extract($category);
$this->response($categoryData, 200);
die;
}
权限:
protected function requirePermission($permission, $assertion = null)
{
if (!$this->rbac->isGranted($this->fixUser->ufUser->getRole(), $permission, $assertion)) {
$this->errorResponse('Permission denied.', 403);
die;
}
return true;
}
403 http 错误代码是框架(和其他框架)在抛出未授权异常时返回的常见错误。
可能来自您应用程序的安全上下文(您的前端是否真正授权或是否需要登录?)
可能是为了内部安全检查,就像你的函数requirePermission('categories.create');
我在尝试 POST 到我的 API 时遇到了这个问题。它 returns 出现以下错误:
Uncaught Error.
{"meta":{"httpCode":403,"message":"Permission denied."},"data":[]}
我发送的授权适用于填充我页面上数据表的 AJAX GET。我想知道是不是我的数据格式有误之类的。 POST 所需的数据只是名称和描述,所以我认为我不会遗漏任何内容。谁能看出此代码中可能导致 403 错误的任何错误?
var name = $('#name').text();
var description = $('#description').text();
$('#add-category').on('click', function() {
$.ajax({
type: "POST",
url: baseUrl + "api/categories",
headers: {
"Authorization": auth
},
data: {
"name": name,
"description": description
},
dataType: 'json',
contentType: 'application/json',
error: function(jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connected.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg);
},
success: function() {
table.draw();
}
});
});
感谢查看!
编辑:这是 API 中的相关函数,我没有看到任何关于 403 错误的信息。也许我应该看看 API?
的其他地方public function categories_post()
{
$this->requirePermission('categories.create');
if (!$postData = $this->post(null, false)) {
$this->errorResponse('Request missing category data in request body.', 405);
}
// @TODO Better Validation.
$postData = $this->xssClean($postData);
$category = $this->gameService->createCategory($postData);
$categoryData = $this->categoryHydrator->extract($category);
$this->response($categoryData, 200);
die;
}
权限:
protected function requirePermission($permission, $assertion = null)
{
if (!$this->rbac->isGranted($this->fixUser->ufUser->getRole(), $permission, $assertion)) {
$this->errorResponse('Permission denied.', 403);
die;
}
return true;
}
403 http 错误代码是框架(和其他框架)在抛出未授权异常时返回的常见错误。
可能来自您应用程序的安全上下文(您的前端是否真正授权或是否需要登录?)
可能是为了内部安全检查,就像你的函数requirePermission('categories.create');