我已经和AngularJS摸索了好几个星期,并试图将这部分,以及另一部分拼凑在一起,以创建一个服务于带有Django后端的Web应用程序的文件。我觉得事情进展顺利,直到我发现自己试图上传包含所有其他表单数据的文件。在发送请求之前,我的HTML表单始终显示为在验证步骤期间没有附加文件。嗯,这不好!无论如何,由于某种原因,这最终成为某种不受支持的操作方式。我转向ng-file-upload
,这是AngularJS的第三方文件上传服务。 ng-file-upload
的最新迭代使用AngularJS 1.6样式请求,而我的第三方注册应用程序angular-django-registration-auth
使用1.6之前的$http
。
我需要更新第三方注册申请,但它有以下代码。
'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 =
开始(这是定义一个承诺对象,对吗?)我不知道发生了什么。在大多数情况下,赋值很容易理解,而promise对象被赋予异常(data = args.data || {};
如何在$http
提供者的复合赋值之一的右手边结束?),但究竟是什么是success
和error
案例中发生angular.bind()
的情况?我似乎无法找到任何好的例子,其中角似乎与承诺绑定。
答案 0 :(得分: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;
},