2016-02-27 48 views
0

这是代码。 我不知道什么是错误的代码,它不工作。我猜测头有一些问题,但语法是正确的。没有-DB(数据库)需要在json中显示输出并将其输出到其他服务器,如“www.hashserver.com”,但使用角度

 <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> 
+0

“它不工作”:请定义“它不工作”是什么意思?你有错误信息吗?什么都没有发生或什么? –

+0

1)Uncaught SyntaxError:missing)参数列表后 angular.js:38未捕获 2)错误:[$ injector:modulerr] http://errors.angularjs.org/1.5.0/$injector/modulerr?p0 = submitExample&P1 =呃...... ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.5.0%2Fangular.min.js%3A20%3A449) – koku19

+0

但我已经检查了所有的语法 – koku19

回答

0

您应该使用$http服务是这样的:可用here

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

的jsfiddle。

$http的文档陈述为here

Shortcut methods are also available. All shortcut methods require passing in the URL, and request data must be passed in for POST/PUT requests. An optional config can be passed as the last argument.

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

+0

获取有关提交值 错误此错误:XMLHttpRequest的不能加载http://.....gpsdb/。对预检请求的响应不会通过访问控制检查:请求的资源上不存在“访问控制 - 允许来源”标头。 Origin'eaxmple.com';因此不允许访问 – koku19

+0

这是因为您要发布的网址与您网页的托管位置不同。有一种称为CORS的机制,可用于控制哪些HTTP动词可以从哪个域使用。但是,必须在服务器端启用,所以如果你不控制服务器,那么你运气不好。 –

+0

服务器接受JSON和我的输出不在JSON是因为该服务器不接受 – koku19

0

语法不正确:$ http.post的第二个参数的语法错误;支架不平衡; $ scope.list出现在无效位置。 您提供的代码会引发JavaScript错误。

而且它不打算通过HTTP 方法当你调用$ http.post,其方法已经在它的名字。

它有助于正确缩进代码以发现其中的一些问题。这里是你可以尝试什么:

$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); 
}); 

然而,你的要求是cross-origin request,将被拒绝如果被叫服务未将您的域名列入白名单。

+0

错误:XMLHttpRequest无法加载http://.....gpsdb/。对预检请求的响应不会通过访问控制检查:请求的资源上不存在“访问控制 - 允许来源”标头。 Origin'http://eaxmple.com'因此不被允许访问。 – koku19

+1

与您的语法错误无关。看看CORS。 https://en.wikipedia.org/wiki/Cross-origin_resource_sharing –

+0

错误消息说明了这一切。被叫网站不允许来自其他域的请求,如您的网站。 – trincot

相关问题