【问题标题】:$http.get doesn't work with REST model using then() function$http.get 不适用于使用 then() 函数的 REST 模型
【发布时间】:2016-03-22 08:06:00
【问题描述】:

众所周知,Angular 最近弃用了 http.get.success,error 函数。因此,不再建议在您的控制器中使用这种调用:

$http.get("/myurl").success(function(data){
    myctrl.myobj = data;
}));

而是要使用这种调用:

$http.get("/myurl").then(
    function(data) {
        myctrl.myobj = data;
    },
    function(error) {
        ...
    }

问题是,简单的 Spring REST 模型不适用于此新代码。我最近下载了一个带有上述旧成功函数和这样的 REST 模型的示例代码:

@RequestMapping("/resource")
public Map<String,Object> home() {
    Map<String,Object> model = new HashMap<String,Object>();
    model.put("id", UUID.randomUUID().toString());
    model.put("content", "Hello World");
    return model;
}

这应该为$http.get() 调用返回一个类似{id:&lt;someid&gt;, content:"Hello World"} 的地图,但它什么也没收到 - 视图是空白的。

我该如何解决这个问题?

【问题讨论】:

  • 用浏览器能搞定吗?
  • 是的,我可以:{"id":"f77e3886-976b-4f38-b84d-ae4d322759d4","content":"Hello World"}
  • 它是否适用于 success() 而不是 then()?
  • 也可以试试console.log(error)
  • @Michelem 与 success() 合作。

标签: angularjs spring rest


【解决方案1】:

传递给success() 的第一个(四个)参数是响应的数据(即正文)。

但是传递给then() 的第一个(也是唯一的)参数不是数据。它是完整的 HTTP 响应,包含数据、标头、状态、配置。

所以你真正需要的是

$http.get("/myurl").then(
    function(response) {
        myctrl.myobj = response.data;
    },
    function(error) {
        ...
    });

【讨论】:

  • 我试试看。
【解决方案2】:

对结果的期望不同。它是响应而不是直接的数据对象。

documentation 说:

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

响应的属性是

data – {string|Object} – The response body transformed with the transform functions.
status – {number} – HTTP status code of the response.
headers – {function([headerName])} – Header getter function.
config – {Object} – The configuration object that was used to generate the request.
statusText – {string} – HTTP status text of the response.

由于需要数据对象,

请将代码转换为

$http.get("/resource").then(
    function(response) {
        myctrl.myobj = response.data;
    });

【讨论】:

    【解决方案3】:

    then 必须返回一个新的 Promise,所以你应该使用 defers 来处理它。

    var myApp = angular.module('myApp', []);
    
    myApp.factory('modelFromFactory', function($q) {
    return {
    
            getModel: function(data) {
          var deferred = $q.defer();
          var items = [];
              items.push({"id":"f77e3886-976b-4f38-b84d-ae4d322759d4","content":"Hello World"});
          deferred.resolve(items);
          return deferred.promise;
      }
    };
    });
    
    function MyCtrl($scope,  modelFromFactory) {
    modelFromFactory.getModel()
        .then(function(data){
            $scope.model = data;
    })
    
    }
    

    这是工作小提琴 -> https://jsfiddle.net/o16kg9p4/7/

    【讨论】:

    • 答案与问题无关。 OP询问处理http请求
    • 我只是想展示一个新的传统,他可以成功地做到这一点,但他要求我们以另一种方式工作。我想帮助他,你不喜欢这么伤心。
    • 你能写东西并不意味着它就一定要写。在提到的情况下,延迟是没有用的
    • 这就是为什么你不是一个开源的人。当您看到新的解决方案时,您应该与他人分享。但是像你这样的人真的很烦我。
    • 你的小提琴不适合我。它一直显示的是{{model}}
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 2016-03-04
    • 2019-10-24
    • 1970-01-01
    • 2019-05-09
    相关资源
    最近更新 更多