[英]Reusable code at checking status respose with Javascript
I'm using Angular in Javascript to connect to the server, What I want to do is make reusable code. 我正在Javascript中使用Angular连接到服务器,我要做的是制作可重用的代码。
The problem if that for each service that have to obtain data from server I have to know status response and if was 200 I have to know if the success was 1 or 0. So... This is my code at today (using promise): 问题是,对于必须从服务器获取数据的每个服务,我必须知道状态响应,如果是200,则必须知道成功是1还是0。所以...这是我今天的代码(使用promise) :
function obtenerUltimasOperacionesComplejo() {
return $http
.get(ultimasOperacionesUrl,
{params: {idComplejo: 11}})
.then(obtenerDatosUltimasOperaciones)
.catch(generarError);
}
function obtenerDatosUltimasOperaciones(data) {
var mensaje = '';
var status = data.status;
if(status == 401){
mensaje = 'Acceso no autorizado.';
return $q.reject(mensaje);
}
else if (status <> 200){
mensaje = 'Ups! Hubo un problema al conectarse al servidor. Intente nuevamente.';
return $q.reject(mensaje);
}
else if(status == 200){
var exito = data.data.success;
if(exito == 0){
mensaje = data.data.mensaje;
return $q.reject(mensaje);
}
else if(exito == 1){
ultimasOperaciones = data.data.ultimasOperaciones;
return ultimasOperaciones;
}
}
}
function generarError(e){
var newMessage = 'XHR error! :';
if (e.data && e.data.description) {
newMessage = newMessage + '\n' + e.data.description;
}
e.data.description = newMessage;
logger.error(newMessage);
return $q.reject('Ups! Hubo un problema al conectarse al servidor. Intente nuevamente');
}
This part of code: 这部分代码:
if(status == 401){
mensaje = 'Acceso no autorizado.';
return $q.reject(mensaje);
}
else if (status <> 200){
mensaje = 'Ups! Hubo un problema al conectarse al servidor. Intente nuevamente.';
return $q.reject(mensaje);
}
else if(status == 200){
var exito = data.data.success;
if(exito == 0){
mensaje = data.data.mensaje;
return $q.reject(mensaje);
}
...
I have to use it for each service that I have... The problem that I have is that I can't put in a function the code above, because I don't know how to set the variable with the corresponding service, I mean: 我必须将其用于我拥有的每个服务...我的问题是我无法在上面的代码中放入函数,因为我不知道如何通过相应的服务来设置变量,意思:
else if(exito == 1){
ultimasOperaciones = data.data.ultimasOperaciones;
return ultimasOperaciones;
}
This part of code changes for each service because "ultimasOperaciones" var is for this service in particular, for another service I have to use another variable and so on... 这部分代码针对每个服务而更改,因为“ ultimasOperaciones”变量特别适用于该服务,对于另一个服务,我必须使用另一个变量,依此类推...
So... There is a way to disjoin this two part of code so I can reuse and don't have to copy and paste the most of the code? 所以...有一种方法可以使代码的这两部分脱节,以便我可以重用,而不必复制和粘贴大部分代码?
Thanks! 谢谢!
please create a interceptor for checking every time the status of your response 请创建一个拦截器,以便每次检查响应状态时
http://thecodebarbarian.com/2015/01/24/angularjs-interceptors http://thecodebarbarian.com/2015/01/24/angularjs-interceptors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.