【问题标题】:$http.get response is undefined$http.get 响应未定义
【发布时间】:2019-01-20 06:27:56
【问题描述】:

我目前正在做前端,后端使用 python。前端是 html,我使用 angularJs 进行通信。

我的问题是我无法从 python 后端获得响应,虽然它被正确调用,但生成了一个列表,我将结果写如下(在我的 python 代码中)

self.write({"success":True, "data":list})

但是,角度代码没有收到任何响应。只返回成功(http 200OK)状态码。如何让后端成功发送列表作为响应?

顺便说一句,角码是

$scope.init = function () {
     //Take the list of applications from database
     var req = {
         method: 'GET',
         url: 'http://localhost:8888/app',
         headers: {
           'Content-Type': "application/json;charset=UTF-8"
         }
     }
     $http(req).success(function(data) {
         $scope.results = response.data
         // response is undefined??
     } )
    .error (.......);

【问题讨论】:

  • 也许试试 console.log(data)?它现在看起来如何响应将是未定义的,因为您只是从函数中获取数据。基本上 .success(function(response)) 与您所拥有的 .success(function(data)) 之间的区别
  • Console.log 给了我 -> 可能未处理的拒绝:{"data":null,"status":-1,"config":{"method":"GET","transformRequest": [null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"localhost:8888/app","headers":{"Accept":"application/json, text/plain, / i>"}},"statusText":"","xhrStatus":"error"}
  • 用函数(响应)替换你的函数(数据),因为现在响应是未定义的,因为它实际上没有在任何地方传递或在任何地方初始化。
  • 仍然未定义,控制台给出相同的输出
  • 也许看看这个codelord.net/2015/05/25/dont-use-%24https-success。显然,更好的方法是使用 .then() 表示法,所以也许可以研究一下。

标签: python angularjs frontend backend


【解决方案1】:

尝试将您的成功回调更改为:

$http(req).success(function(response) {
         $scope.results = response.data;
})

您在成功回调方法中将参数捕获为名为 data 的变量,并尝试访问方法中的 response 变量,该变量实际上未定义。

【讨论】:

  • 谢谢,但是当我这样做时,这个响应也是未定义的,没有任何改变。
  • 然后,在您的浏览器上捕获网络流量,并验证您的 REST 调用在这种情况下是否实际返回任何响应对象
  • 是的,我也这样做了,响应随心所欲,{"success":true, "apps" : { ....(app list)....} } 返回。但是 angularjs 似乎不知道这个响应...
  • console.log(response) 给你什么?
  • 它给出了 Object { data: null, status: -1, headers: zd(), config: {...}, statusText: "", xhrStatus: "error" } 并且响应现在返回 HTTP405选项响应..
猜你喜欢
  • 2018-03-11
  • 1970-01-01
  • 2021-06-21
  • 2021-05-26
  • 2021-10-28
  • 2014-12-30
  • 2012-06-09
  • 1970-01-01
相关资源
最近更新 更多