好吧,我现在已经尝试了2个小时,并且无法完成这项工作。 dropzone无法上传任何文件。服务器说"令牌未提供"。即使用laravel作为后端,它使用jwt令牌进行身份验证,使用角度作为前端。这是我的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']);
}));
和控制器方法
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(Request $request)
{
$file = Input::file('file');
return 'okay'; // just until it works
}
令牌是正确的并且实际上是到达服务器(因为我尝试在另一个控制器函数中使用Input :: get(' token')返回令牌并且它正常工作)。谁能告诉我我做错了什么?即时通讯" 400 Bad Request"用" token_not_provided"信息... 谢谢你的帮助。我为我糟糕的英语道歉..
答案 0 :(得分:3)
我不确定为什么将令牌附加到表单不起作用,但您可以尝试在授权标头中发送它。
替换
formData.append('token', TokenHandler.getToken());
使用
xhr.setRequestHeader('Authorization', 'Bearer: ' + TokenHandler.getToken());
答案 1 :(得分:0)
确保将令牌添加到您的调用中:例如,您可以将toke作为参数添加到dropzone url参数中。
//If you are using satellizer you can you this
var token = $auth.getToken();// remember to inject $auth
$scope.dropzoneConfig = {
options: { // passed into the Dropzone constructor
url: 'http://localhost:8000/api/attachments?token=token'
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);
}
}
};