如何通过post方法将数据添加到现有的json对象?

时间:2016-02-26 11:08:06

标签: angularjs json ionic-framework

我有一个json对象user.js

[{
    "id": 1,
    "username": "vishnu",
    "password": "password"
},{
    "id": 2,
    "username": "vishnu2",
    "password": "password"
},{
    "id": 2,
    "username": "vishnu@gmail.com",
    "password": "password"
}]

我已经创建了一个服务来访问它的数据,现在我想通过post方法使用相同的服务向它添加数据。帮助我。

angular.module('starter.service')
.factory('authService', function($http) {
  var service = {};

  service.GetByUsername = function() {
    return $http.get('/js/user.json');
  }

  return service;
})
如果可以,

服务在相同的代码中给出above.edit。

添加

    "id": 3,

    "username": "yourself@gmail.com",

    "password": "password"

1 个答案:

答案 0 :(得分:0)

默认POST http呼叫:

  $http({
       method: 'POST',
       url: 'URL',
      data: yourJsonObject
    }).success(function (data, status) {
    }).error(function (data, status) {
    });
相关问题