2

我正在使用 Box API 构建一个 Web 应用程序,但我无法在根目录中创建一个文件夹。令牌设置为应处于活动状态的开发人员令牌。

我现在收到的错误是Bad Request. 这段代码有什么问题?我也无法接收用户的身份验证,但我决定先解决这个问题。

function createTestFolder() {
    $.ajax('https://api.box.com/2.0/folders', 
    {
        data: { name: 'CreatedFolderFromjQuery', parent: { id: '0' } },
        type: 'POST',
        beforeSend: function(xhr) {
            xhr.setRequestHeader('Authorization', 'Bearer ' + window.token);
        },
        contentType: 'json',
        success: function(data, status, xhr) {
            alert(status);
        },
        error: function(xhr, status, error) { alert(error); }
    });
}

编辑: 当我将 URL 更改为 时https://box.com/api/1.0/folders,我似乎得到了成功的响应。也就是说,success调用的是函数,而不是error函数。但是,该文件夹仍未上传到我的帐户。

编辑 2:使用 curl 命令行并遵循 API 文档,我仍然收到相同的错误消息。以下是详细信息:

{"type":"error", "status":400, "code":"bad_request", "context_info":{"errors":[{"reason":"invalid_parameter", "name":"entity_body", "message":"Invalid value ''{name:New Folder,'. Entity body should be a correctly nested resource attribute name/value pair"}]}, "help_url":"http://developers.box.com/docs/#errors", "message":"Bad Request", "request_id":"128521198353f4fc831c7e6"}
curl: (6) Could not resolve host: parent
curl: (3) [globbing] unmatched brace in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 2
4

1 回答 1

2

啊,我已经解决了我自己的问题。我不熟悉 jQuery,这是我的错误:

data: { name: 'CreatedFolderFromjQuery', parent: { id: '0' } },

使用 jQuery 的 AJAX API,data需要是一个字符串。我需要将我的 POST 数据序列化为 JSON:

data: JSON.stringify({ name: 'CreatedFolderFromjQuery', parent: { id: '0' } }),

一旦我意识到这一点,并添加JSON.stringify()请求已正确发送。

于 2014-08-20T20:34:07.970 回答