我正在使用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
啊,我已经解决了我自己的问题。我不熟悉jQuery,这是我的错误:
data: { name: 'CreatedFolderFromjQuery', parent: { id: '0' } },
使用jQuery的AJAX API,data
需要为字符串。我需要将POST数据序列化为JSON:
data: JSON.stringify({ name: 'CreatedFolderFromjQuery', parent: { id: '0' } }),
一旦意识到这一点,并添加JSON.stringify()
了请求,就可以正确发送该请求。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句