我得到一个错误如何使用angularjs将新数据写入现有的json文件?

时间:2015-09-15 06:44:14

标签: javascript json angularjs

我收到错误,如何使用angularjs将新数据写入现有的JSON文件?我使用$http.post方法将新数据添加到我的文件中,但它没有添加,但我在警告框中收到了成功消息。

如果我更改了文件的名称,则会在警告中显示失败的消息。

mymodal.controller('MainCtrl', function ($scope,$http) { 
                        var url="customer.json";
                        $http.get(url).success( function(data) {
                        $scope.customers = data;
                      });
                       $scope.addUser=function(name,email,phone){
                       var dataObject = {
                       Name : "robin"
                      ,email  : "robin"
                      ,password  : 123,
                      phone:123
                    };

                    var write = $http.post("customer.json", dataObject);
                    write.success(function(data, status, headers, config) {
                      console.log(data.title);
                      alert("Submitting form success!");

                    });
                    write.error(function(data, status, headers, config) {
                      alert("Submitting form failed!");
                    }); 
                    }
                    };
                 // am getting the success message but its not adding         the                new        data into customer.json file

                    here is my json file
                    ====================
                    [
                    {
                    "Name" : "Mahesh Parashar",
                    "email" : "abc@gmail.com",
                    "password" : 123,
                    "phone":1111
                    },
                    {
                    "Name" : "Dinkar Kad",
                    "email" : "def@gmail.com",
                    "password" : 123,
                    "phone":2222
                    },
                    {
                    "Name" : "Robert",
                    "email" : "jkl@gmail.com",
                    "password" : 123,
                    "phone":333
                    },
                    {
                    "Name" : "Julian Joe", 
                    "email" : "xyz@gmail.com",
                    "password" : 123,
                    "phone":444
                    }
                    ]//this is ma customer.json file

0 个答案:

没有答案