【问题标题】:Want to send form json data to other server想要将表单 json 数据发送到其他服务器
【发布时间】:2016-02-25 07:29:14
【问题描述】:

我有使用 Angular 以 json 格式输出数据的表单,没有数据库。现在无论该表单的输出是什么,我都想将其发送到某个服务器这是代码,例如我想将其发送到服务器 192.80.36.4。如何使用帖子来做到这一点

 controller definition code 


<body ng-app="submitExample">
  <script>
    angular.module('submitExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
     $scope.list = [];
     $scope.text = '';
     $scope.submit = function() {
     if ($scope.text) {
      $scope.list.push(this.text);
      $scope.text = '';
    }
   };
  }]);
     </script>

  </script>
  <form ng-submit="submit()" ng-controller="ExampleController">
        Enter latitude:
        <input type="text" ng-model="text" name="text" />
        <input type="submit" id="submit" value="Submit" />
        <pre>list={{list| json}}</pre>
  </form>

【问题讨论】:

    标签: angularjs json angularjs-scope http-post form-submit


    【解决方案1】:

    您需要使用$http服务:

    angular.module('submitExample', [])
      .controller('ExampleController', ['$scope', '$http', function($scope, $http) {
        $scope.list = [];
        $scope.text = '';
        $scope.submit = function() {
          if ($scope.text) {
            $scope.list.push(this.text);
            $scope.text = '';
            $http.post("<your-url>", $scope.list).success(function(data, status) {
              console.log(data);
            })
          }
        };
      }]);
    

    【讨论】:

    • 我要将数据发送到的服务器的url?
    • 我不知道您必须将数据发送到哪里。您需要一个能够捕获您的 POST 请求的服务器。
    • XMLHttpRequest 无法加载 183.000.00.00/t/db。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'example.com' 不允许访问。这是错误
    • 似乎 post 请求不是 json 类型,它是 XML
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多