Angularjs 如何从控制器中知道是否为http.post()。然后。。。成功了吗?
我有一个控制器,它使用以下行通过名为Angularjs 如何从控制器中知道是否为http.post()。然后。。。成功了吗?,angularjs,ionic-framework,angularjs-service,angularjs-controller,angular-controller,Angularjs,Ionic Framework,Angularjs Service,Angularjs Controller,Angular Controller,我有一个控制器,它使用以下行通过名为SendDataFactory的工厂将数据发布到服务器: SendDataFactory.sendToWebService(dataToSend) 我的工厂SendDataFactory如下所示: angular .module('az-app') .factory('SendDataFactory', function ($http, $q) { var SendData = {}; /** * Sends data
SendDataFactory
的工厂将数据发布到服务器:
SendDataFactory.sendToWebService(dataToSend)
我的工厂SendDataFactory
如下所示:
angular
.module('az-app')
.factory('SendDataFactory', function ($http, $q) {
var SendData = {};
/**
* Sends data to server and
*/
SendData.sendToWebService = function (dataToSend) {
var url = "example.com/url-to-post";
var deferred = $q.defer();
$http.post(url, dataToSend)
//SUCCESS: this callback will be called asynchronously when the response is available
.then(function (response) {
console.log("Successful: response from submitting data to server was: " + response);
deferred.resolve({
data: data
});
},
//ERROR: called asynchronously if an error occurs or server returns response with an error status.
function (response) {
console.log("Error: response from submitting data to server was: " + response);
deferred.resolve({
data: data
});
}
);
return deferred.promise;
}
return SendData;
});
我在这里和互联网上看到了一些例子
$http.post().success...
但是我想用
$http.post().then...
自:
$http legacy promise方法success和error已被弃用。改用标准then方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$http/legacy错误
我需要什么:
现在,在我的控制器中,我需要检查
$http.post()。然后…
是否成功,然后根据成功或失败进行操作。我怎样才能做到这一点?我想这就是你的意思:
$http.post(url, dataToSend)
//SUCCESS: this callback will be called asynchronously when the response is available
.then(function (response) {
console.log("Successful: response from submitting data to server was: " + response);
deferred.resolve({
data: response //RETURNING RESPONSE SINCE `DATA` IS NOT DEFINED
});
},
//ERROR: called asynchronously if an error occurs or server returns response with an error status.
function (response) {
console.log("Error: response from submitting data to server was: " + response);
//USING THE PROMISE REJECT FUNC TO CATCH ERRORS
deferred.reject({
data: response //RETURNING RESPONSE SINCE `DATA` IS NOT DEFINED
});
}
);
return deferred.promise;
}
在控制器中,您现在可以使用:
SendDataFactory.sendToWebService(dataToSend)
.then(function(data) { /* do what you want */ })
.catch(function(err) { /* do what you want with the `err` */ });
拒绝承诺,而不是在被
$http
拒绝时解决承诺
/**
* Sends data to server and
*/
SendData.sendToWebService = function (dataToSend) {
var url = "example.com/url-to-post";
var deferred = $q.defer();
$http.post(url, dataToSend)
//SUCCESS: this callback will be called asynchronously when the response is available
.then(function (response) {
console.log("Successful: response from submitting data to server was: " + response);
deferred.resolve(response.data); // Resolving using response.data, as data was not defined.
},
//ERROR: called asynchronously if an error occurs or server returns response with an error status.
function (response) {
console.log("Error: response from submitting data to server was: " + response);
deferred.reject(response.data); // Rejecting using response.data, as data was not defined.
}
);
return deferred.promise;
}
然后,您可以使用then
从控制器调用它,方法与处理服务中的回调相同
由于$http
返回承诺,因此可以通过使用承诺链进一步简化。这样就不需要使用额外的延迟对象
/**
* Sends data to server and
*/
SendData.sendToWebService = function (dataToSend) {
var url = "example.com/url-to-post";
return $http.post(url, dataToSend)
//SUCCESS: this callback will be called asynchronously when the response is available
.then(function (response) {
console.log("Successful: response from submitting data to server was: " + response);
return response.data; // Resolving using response.data, as data was not defined.
},
//ERROR: called asynchronously if an error occurs or server returns response with an error status.
function (response) {
console.log("Error: response from submitting data to server was: " + response);
return $q.reject(response.data); // Rejecting using response.data, as data was not defined.
}
);
}
您不使用“延迟”的任何原因。拒绝使用“延迟”?非常感谢:这很有效!有什么方法可以使用
.catch(函数(err){/*使用err`*/})吗要知道服务器返回的是哪种类型的错误,例如超时或404?当然,您可以检查响应上的状态
或状态码
,类似于:.catch(函数(err){console.log(err.statusCode)})代码>??没错,但我建议您控制台记录错误并进行调查,我尝试过:.catch(function(err){console.log(err.status);}代码>但没有打印任何内容