好吧,我已经尝试了大约 2 个小时,但无法完成这项工作。 dropzone 无法上传任何文件。服务器显示“未提供 token ”。我使用 Laravel 作为后端,它使用 jwt token 进行身份验证,并使用 Angular 作为前端。这是我的 dropzone 配置。
$scope.dropzoneConfig = {
options: { // passed into the Dropzone constructor
url: 'http://localhost:8000/api/attachments'
paramName: 'file'
},
eventHandlers: {
sending: function (file, xhr, formData) {
formData.append('token', TokenHandler.getToken());
console.log('sending');
},
success: function (file, response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
}
};
和路由定义
Route::group(array('prefix' => 'api', 'middleware' => 'jwt.auth'), function() {
Route::resource('attachments', 'AttachmentController', ['only' => 'store']);
}));
和 Controller 方法
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(Request $request)
{
$file = Input::file('file');
return 'okay'; // just until it works
}
token 是正确的并且实际上正在到达服务器(因为我尝试在另一个 Controller 函数中使用 Input::get('token') 返回 token 并且它有效)。有人可以告诉我我做错了什么吗?我收到“400 错误请求”和“token_not_provided”消息... 谢谢你的帮助。我为我糟糕的英语道歉..
最佳答案
我不确定为什么将 token 附加到表单不起作用,但您可以尝试在授权 header 中发送它。
替换
formData.append('token', TokenHandler.getToken());
与
xhr.setRequestHeader('Authorization', 'Bearer: ' + TokenHandler.getToken());
关于php - dropzone 未上传,400 错误请求,token_not_provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32912084/