私は数週間、AngularJSに悩まされており、これと他のものを一緒にしてファイル配信Webアプリケーションを作成していますDjangoバックエンドを使っています。他のすべてのフォームデータを含むファイルをアップロードしようとするまで、うまくいっていると思っていました。私のHTMLフォームは、リクエストを送信する前に検証ステップ中にファイルが添付されていないものとして一貫して現れました。まあ、それは良いことではありません!とにかく、これは何らかの理由で、サポートされていない操作の何らかの方法であった。 AngularJSのサードパーティのファイルアップロードサービスであるng-file-upload
に目を向けると、 ng-file-upload
の最新の反復では、AngularJS 1.6スタイルのリクエストが使用され、サードパーティの登録アプリケーションangular-django-registration-auth
は、012の前に、$http
を使用します。AngularJS:djangoバックエンドを使用して1.5.xから1.6+にhttpを移行する
第三者登録アプリケーションを更新する必要がありますが、次のコードがあります。 var deferred =
で始まり
'request': function(args) {
// Let's retrieve the token from the cookie, if available
if($cookies.token){
$http.defaults.headers.common.Authorization = 'Token ' + $cookies.token;
}
// Continue
params = args.params || {}
args = args || {};
var deferred = $q.defer(),
url = this.API_URL + args.url,
method = args.method || "GET",
params = params,
data = args.data || {};
// Fire the request, as configured.
$http({
url: url,
withCredentials: this.use_session,
method: method.toUpperCase(),
headers: {'X-CSRFToken': $cookies['csrftoken']},
params: params,
data: data
})
.success(angular.bind(this,function(data, status, headers, config) {
deferred.resolve(data, status);
}))
.error(angular.bind(this,function(data, status, headers, config) {
console.log("error syncing with: " + url);
// Set request status
if(data){
data.status = status;
}
if(status == 0){
if(data == ""){
data = {};
data['status'] = 0;
data['non_field_errors'] = ["Could not connect. Please try again."];
}
// or if the data is null, then there was a timeout.
if(data == null){
// Inject a non field error alerting the user
// that there's been a timeout error.
data = {};
data['status'] = 0;
data['non_field_errors'] = ["Server timed out. Please try again."];
}
}
deferred.reject(data, status, headers, config);
}));
return deferred.promise;
},
(これは正しい、約束のオブジェクトを定義している?)私は何が起こっているかについては不明です。代入はほとんどの場合、約束事オブジェクトには例外があります(data = args.data || {};
は$http
プロバイダの複合代入のどれかの右辺になります)。しかし、success
とerror
のケースではどういうことが起こっていますかここではangular.bind()
と呼ばれていますか?私は角度が約束に結び付いているような良い例は見つけられないようです。