需要以 json 显示输出,并将其发送到其他服务器说“www.hashserver.com”但使用角度


Need to display output in json and it to other server say "www.hashserver.com" but using angular

这是代码。我不知道代码不起作用有什么问题。我想标题定义存在一些问题,但语法是正确的。没有数据库(数据库)

     <script>
 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("www.sendtohashserver.com/db",method: "POST", {headers: {'Content-Type': 'application/json'} $scope.list).success(function(data, status) {
      console.log(data);
        })
      }
    };
  }]);
</script>
<form ng-submit="submit()" ng-controller="ExampleController">
  Enter Name:
  <input type="text" ng-model="text" name="text" />
  <input type="submit" id="submit" value="Submit" />
  <pre>list={{list| json}}</pre>
</form>

你应该像这样使用$http服务:

$http.post("www.sendtohashserver.com/db", $scope.list, {
    headers: { 'Content-Type': 'application/json' } 
}).success(function(data, status) {
    console.log(data);
});

JSFiddle可在此处获得。

$http的文档在此处说明了这一点。

快捷方法也可用。所有快捷方法都需要传入 URL,并且必须传入 POST/PUT 请求的请求数据。可选配置可以作为最后一个参数传递。

$http.get('/someUrl', config).then(successCallback, errorCallback); $http.post('/someUrl', data, config).then(successCallback, errorCallback);

语法不正确:$http.post 的第二个参数语法错误; 括号不平衡;$scope.list 出现在无效位置。您的代码,正如您提供的那样,将引发 JavaScript 错误。

此外,当您调用 $http.post 时,它不打算传递 http 方法,因为它的名称中已经有该方法。

它有助于正确缩进代码以发现其中一些问题。以下是您可以尝试的方法:

$http.post(
    "www.sendtohashserver.com/db",
    $scope.list,
    {
        headers: {'Content-Type': 'application/json'}
    } 
).success(function(data, status) {
    console.log(data);
});

如果您调用的服务器请求数据采用 JSON 字符串格式,则调用 JSON.stringify

$http.post(
    "www.sendtohashserver.com/db",
    JSON.stringify($scope.list),
    {
        headers: {'Content-Type': 'application/json'}
    } 
).success(function(data, status) {
    console.log(data);
});

但是,您的请求是跨域请求,如果被调用服务未将您的域列入白名单,则会被拒绝。