【问题标题】:AngularJS post model to a json file using $http.post()AngularJS 使用 $http.post() 将模型发布到 json 文件
【发布时间】:2014-12-11 07:45:07
【问题描述】:

我是 AngularJS 的新手。我想将注册数据发布到 json 文件。发布请求工作正常,但数据未写入文件。我无法理解那个立场。我的发布请求代码有问题吗?我该如何解决?谢谢!

 var regModel = {
    FirstName: 'Someone',
    LastName: 'Somebody',
    Address: 'somewhere',
    Email: 'something@gmail.com'
};

$scope.insertUser = function () {
    $http.post('../DB/users.json', regModel).
        success(function(data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available
        }).
        error(function(data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
}

【问题讨论】:

    标签: javascript json angularjs http


    【解决方案1】:

    使用您当前的设置,您的发布请求将在您的请求内容中包含您的 regModel 对象。在服务器端,你可以获取正文或内容,然后将其写入文件。

    【讨论】:

    • 这意味着我不能在客户端写入文件,是吗?
    • 我不知道客户端有任何已知方式。
    【解决方案2】:

    您不能从客户端直接写入位于服务器上的文件。 正如 willwin.w 所说,您的数据 regData 应该与请求一起传输。但是您必须将您的 ajax 请求指向 PHP、ASP 等服务器端脚本。然后,该脚本必须获取 ajax 请求中传递的数据并将它们写入文件或数据库中。

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 2015-07-25
      • 2016-02-23
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多