简体   繁体   English

Angular 1-解析$ http响应

[英]Angular 1 - parsing an $http response

I am new to Javascript and Angular. 我是Java和Angular的新手。 I am trying to move from an $httpbackend mock server using straight JSON data from a file like [{"firstName":"James",...}...] to getting the data from a live REST server to try out my client code (backand). 我正在尝试从使用[{[firstName]:“ James”,...} ...]等文件的直接JSON数据从$ httpbackend模拟服务器转移到从实时REST服务器获取数据来尝试我的客户代码(backand)。

On my client I use the http service as follows: 在我的客户端上,我使用http服务,如下所示:

return $http.get(serviceUrlBase).then(getAllcontactsSuccess, getAllcontactsError);
// Promises
function getAllcontactsSuccess(response)
{
  return response.data.data;
}

The 'response data.data' returns this:
{"data":[{"__metadata":{"id":"1"},"id":1,"firstName":"James","lastName":"Bud","email":"jbud@gmail.com","phone":"504-621-8927","street":"6649 N Blue Gum St","city":"New Orleans","state":"LA","zip":"70116"},{"__metadata":{"id":"2"},"id":2,"firstName":"Josephine","lastName":"Darakjy","email":"josephine_darakjy@darakjy.org","phone":"810-374-9840","street":"4 B Blue Ridge Blvd","city":"Brighton","state":"MI","zip":"48116"},...]}

I read a lot of similar questions and I am trying to write an httptransformer but whatever I try is not working. 我读了很多类似的问题,并且尝试编写一个httptransformer,但是无论如何我都无法正常工作。 I cant get rid of the "__metadata":{"id":"1"}, section which seem to be the issue as it is seen as a additional field when carrier over in the controller. 我无法摆脱“ __metadata”:{“ id”:“ 1”}这一节,这似乎是个问题,因为它在控制器中移载时被视为一个附加字段。

Any ideas is appreciated. 任何想法表示赞赏。

Well you should mock the data to be the same as the actual service call, but in the service call, you can do something like: 好吧,您应该模拟数据使其与实际服务调用相同,但是在服务调用中,您可以执行以下操作:

data = response.data.data
data.forEach(function (d) {
  delete d.__metadata
})

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

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