Patrick R Patrick R - 1 year ago 107
AngularJS Question

Angular 1 - parsing an $http response

I am new to Javascript and 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).

On my client I use the http service as follows:

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

The 'response' returns this:
{"data":[{"__metadata":{"id":"1"},"id":1,"firstName":"James","lastName":"Bud","email":"","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":"","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. 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.

Any ideas is appreciated.

Answer Source

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 =
data.forEach(function (d) {
  delete d.__metadata
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download