2017-04-25 14 views
0

私は数週間、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プロバイダの複合代入のどれかの右辺になります)。しかし、successerrorのケースではどういうことが起こっていますかここではangular.bind()と呼ばれていますか?私は角度が約束に結び付いているような良い例は見つけられないようです。

答えて

0

適切なリソースを見つけてthen()コールでこれを修正しました。ここで私のコードは、誰か他の人を助けるかもしれないので、私のロギングを含むように見えるようになったものです。

"request": function(args) { 
// Let"s retrieve the token from the cookie, if available 
    if($cookies.get("token")){ 
    $http.defaults.headers.common.Authorization = "Token " + $cookies.get("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 
}) 
    .then(function(response) { 
    console.log("Success case: " + url); 
    console.log("Headers: " + JSON.stringify(response.headers(),null, 4)); 
    console.log("Config: " + response.config); 
    console.log("Status: " + response.status); 
    console.log('Response: '); 
    console.log('JSON: ' + JSON.stringify(response.data, null, 4)); 
    deferred.resolve(response.data, response.status); 
    }, function(response) { 
    console.log("Error case: " + url); 
    console.log("Headers: " + JSON.stringify(response.headers(),null, 4)); 
    console.log("Config: " + response.config); 
    console.log("Status: " + response.status); 
    console.log('Response: '); 
    console.log('JSON:' + JSON.stringify(response.data, null, 4)); 

    if(response.data){ response.data.status = status; } 
    if(status == 0){ 
     if(response.data == ""){ 
     response.data = {}; 
     response.data["status"] = 0; 
     response.data["non_field_errors"] = ["Could not connect. Please try again."]; 
     } 
     if(data == null){ 
     response.data = {}; 
     response.data["status"] = 0; 
     response.data["non_field_errors"] = ["Server timed out. Please try again."]; 
     } 
    } 
    deferred.reject(response.data, response.status, response.headers, response.config); 
    }); 
    return deferred.promise; 
}, 
関連する問題