简体   繁体   English

Angular HTTP响应长度未定义

[英]Angular http response length undefined

I'm using Angular to interact with jQuery Mobile and create several pages but am running into an issue with the $http service when connecting to my JSON requests. 我正在使用Angular与jQuery Mobile交互并创建多个页面,但是在连接到JSON请求时遇到了$ http服务问题。 I have a few arrays I'm populating with functions and can bring back data to my pages, except at times I will get errors like this 我用函数填充了一些数组,可以将数据带回到我的页面,但有时会出现类似这样的错误

Cannot read property 'length' of undefined 无法读取未定义的属性“长度”

My code looks like this: 我的代码如下所示:

$http.jsonp(url).success(function(data) {
  for (i=0; i<data.PROJECT.length; i++)
      $scope.ProjectList.push( { id:data.PROJECT[i].PROJECT_CODE, name:data.PROJECT[i].PROJECT_DESC } );
});

My For loop is where it will randomly crash. 我的For循环是随机崩溃的地方。 I've also tried a "then" approach: 我还尝试了一种“然后”的方法:

var request = $http.jsonp(url);
return request.then(function(request) { return request.data.PROJECT; }, handleError );

Same error (or no error and blank results). 错误相同(或没有错误,结果为空白)。 What am I doing wrong? 我究竟做错了什么? I have each API call in it's own function so it shouldn't be reusing objects (might it be appending them though?). 我在其自己的函数中都有每个API调用,因此它不应该重用对象(但是可能会附加对象吗?)。 This is part 1; 这是第1部分; part 2 is separating my code into controller/service - I'm getting these same errors when I do that as well. 第2部分将我的代码分为控制器/服务-当我这样做时也遇到了同样的错误。

UPDATE UPDATE

I've created a jsFiddle here for showing how the values will populate SOMETIMES - run it a few times and you'll notice the arrays take turns returning empty. 我在这里创建了一个jsFiddle 用于显示值如何在某些时候填充-运行几次,您会注意到数组轮流返回空值。 If you were able to see the console.log output you would see ProjectList contains the resultset of EquipList (in addition to ProjectList) and vice versa. 如果您能够看到console.log输出,则将看到ProjectList包含EquipList的结果集(除了ProjectList),反之亦然。

Sorry SO doesn't let me comment yet :-S so I'll answer here. 抱歉,我还没有让我评论:-S,所以我在这里回答。 Based on your 根据您的

... except at times I will get errors like this ...除了有时我会得到这样的错误

it sounds like the URL you are calling isn't responding correctly. 听起来您正在呼叫的网址未正确响应。 The code looks good assuming your JSON response looks like this: 假设您的JSON响应如下所示,代码看起来不错:

{
  "PROJECT": [ 
    { "PROJECT_CODE": ..., "PROJECT_DESC": ... },
    ...
  ]
}

However if you have this intermittently I would guess that the server responds with something other than this structure above, however still sending a HTTP status 200 OK . 但是,如果您断断续续地拥有此服务器,则可能会认为服务器以上述结构以外的方式进行响应,但是仍会发送HTTP状态200 OK

Try to monitor your network requests and see what the actual response is, or do a console.log of data inside the success function. 尝试监视您的网络请求,看看实际的响应是什么,或者在success函数中执行console.log data

--- EDIT -编辑

As I thought it mainly is a API issue. 我认为这主要是API问题。 The API seems to respond sometimes with the correct list(s), but often times (at least now) it is responding with a HTTP 200 OK , but without any data ( Content-Length: 0 ). 该API有时有时会使用正确的列表进行响应,但有时(至少现在是)它使用HTTP 200 OK响应,但没有任何数据( Content-Length: 0 )。 Your fiddle doesn't really show when it's loading, so I updated your code a little bit here: http://jsfiddle.net/gce4ns9a/15/ . 您的小提琴在加载时并没有真正显示出来,因此我在这里更新了您的代码: http : //jsfiddle.net/gce4ns9a/15/ The reason why you never have that 'This never shows up' is because the API responds with 'OK' instead of some error code such as HTTP 500 Inter server error or something similar. 之所以没有“永不显示”,是因为API的响应是“ OK”,而不是诸如HTTP 500 Inter server error或类似错误之类的错误代码。

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM