2014-10-07 73 views
0

发布http请求时,我无法捕获错误函数回调中的http错误。在角js中捕获http错误

下面是客户端:

$http.post('/updateBuildings', 
      $.param(
       {'element_id': element_id} 
      )).success(function(data) { 
       tmp.GetBuildings(data.new_building_id);   
      }).error(function(data) { 
       console.log('response: ',data); 
      }); 

    }; 

服务器端是故意返回一个HTTP错误:状态代码:401,味精:“很抱歉,您 拒绝”

http error written by browser to console

浏览器(谷歌浏览器)本身写入日志(见附件),但我没能赶上角误差并提出警告用户为例。

任何想法?

感谢

+0

http://stackoverflow.com/questions/22113286/prevent-http-errors-from-being-logged-in-browser-console – 2014-10-07 06:55:29

+0

我不想阻止日志浏览器。我想也处理角度的错误 – 2014-10-07 07:03:26

+0

然后尝试catch块将是我想要的方式。 – 2014-10-07 07:12:16

回答

1

我使用successerror有问题:他们没有表现得很,因为我对于期望的承诺链接和错误。虽然我不确定这是怎么回事,但我建议不要使用successerror,但使用标准承诺then/catch函数。

$http.post('/updateBuildings', $.param({ 
    'element_id': element_id 
})).then(function(response) { 
    return tmp.GetBuildings(response.data.new_building_id);   
}).catch(function(error) { 
    console.log('response: ', error.data); 
}); 
+0

试过,但仍然没有在角码中捕获错误 – 2014-10-07 11:02:59