2016-02-26 83 views
1

我有一個JSON對象user.js的如何通過post方法將數據添加到現有的json對象?

[{ 
    "id": 1, 
    "username": "vishnu", 
    "password": "password" 
},{ 
    "id": 2, 
    "username": "vishnu2", 
    "password": "password" 
},{ 
    "id": 2, 
    "username": "[email protected]", 
    "password": "password" 
}] 

和我創建了一個服務從它訪問數據,現在我想通過使用後相同的服務來添加數據method.help我。如果U可以

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": "[email protected]", 

    "password": "password" 

回答

0

默認POST HTTP調用:

$http({ 
     method: 'POST', 
     url: 'URL', 
     data: yourJsonObject 
    }).success(function (data, status) { 
    }).error(function (data, status) { 
    }); 
+0

Ÿ成功的載體作用有2個parametrs? –

+0

https://docs.angularjs.org/api/ng/service/$http。數據, 狀態,標題,配置,statusText。如果它有幫助,upvote和標誌爲解決方案的歡呼聲 – CodeNashor

相關問題