$ resource HTTP状态AngularJs

时间:2016-04-26 13:59:05

标签: angularjs

我是AngularJs的新手,我正在努力真正地思考如何使用$ resource来获取传回的HTTP状态代码我有这个工厂我设置了一种方法我试图实现获取HTTP标头没有成功。

app.js

Sub Workbook_RefreshAll()
ActiveWorkbook.RefreshAll
End Sub

1 个答案:

答案 0 :(得分:2)

如果您是Angularjs的新手,我建议您使用$ http服务:

https://docs.angularjs.org/api/ng/service/ $ HTTP

// Simple GET request example:
$http({
  method: 'GET',
  header:{"content-type":"text"},
  url: '/baseUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available

          console.log(response.status)

  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

否则你需要使用一个拦截器,但我的观点目前是过度杀伤:

.factory("Resource", function ($resource, baseUrl) {
    var resource = $resource(url, {}, {
        get: {
            method: 'GET'
            interceptor: {
                response: function(response) {      
                    var result = response.resource;        
                    result.$status = response.status;
                    return result;
                }
            }
        }                            
    });

return resource;
});

你可以从工厂退货。