AJAX POST 403权限拒绝了错误 - 即使我正在发送验证标题



在尝试发布到我的API时,我会遇到此问题。它返回以下错误:

Uncaught Error.
{"meta":{"httpCode":403,"message":"Permission denied."},"data":[]}

我正在为Ajax发送的auther,可以在我的页面上填充数据。我想知道我的数据是错误的格式还是其他。帖子所需的数据只是名称和描述,因此我不相信我把任何东西都留在那里。谁能在此代码中看到任何可能引起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');

最新更新