2017-04-25 56 views
0

我一直在使用AngularJS摸索數週,並試圖將這部分內容與其他內容一起拼湊起來,以創建文件服務Web應用程序與Django後端。我認爲事情進展順利,直到我發現自己試圖上傳帶有我所有其他表單數據的文件。我的HTML表單始終顯示爲在發送請求之前在驗證步驟中沒有附加任何文件。那麼,這不好!無論如何,這最終成爲某種不支持的操作方式,出於某種原因。我轉向ng-file-upload,這是AngularJS的第三方文件上傳服務。 ng-file-upload的最新迭代使用AngularJS 1.6風格的請求,我的第三方註冊應用angular-django-registration-auth使用1.677之前的$httpAngularJS:使用django後端將http從1.5.x遷移到1.6+

我需要更新第三方註冊應用程序,但它具有以下代碼。

'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; 
}, 

var deferred =開始(這是定義一個承諾對象,對不對?)我對正在發生的事情不清楚。這些任務大部分都很容易理解,除了授予承諾對象外(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; 
},