如何通過AngularJS 在這裏發表JSON數據網絡服務是代碼片段如何JSON數據發佈到web服務REST在AngularJS
.controller('MessagePostCtrl', function($scope, $http) {
$scope.postMessage = function() {
var msg = document.getElementById('message').value;
var msgdata = {
message : msg
};
var res = $http.post('http://<domain-name>/messenger/api/posts/savePost',msgdata);
res.success(function(data, status, headers, config) {
console.log(data);
});
}
})
OPTIONS HTTP:///信使/ API /職位/ savePost
ionic.bundle.js:16185(匿名功能)ionic.bundle.js:16185個 sendReq ionic.bundle.js:15979個serverRequest ionic.bundle.js:15712個 wrappedCallback ionic.bundle.js:19197 wrappedCallback ionic.bundle.js:19197(匿名函數)ionic.bundle.js:19283 範圍。$ eval ionic.bundle.js:20326範圍。$ digest ionic.bundle.js:20138 範圍。$ apply ionic.bundle.js:20430(匿名函數) ionic.bundle.js:43025(匿名函數) ionic.bundle.js:10478 的forEach ionic.bundle.js:7950事件處理ionic.bundle.js:10477個 triggerMouseEvent ionic.bundle.js:2648 tapClick ionic.bundle.js:2637 tapMouseUp ionic.bundle.js:2707XMLHttpRequest無法加載 http:/// messenger/api/posts/savePost。無效的HTTP 狀態碼404
但當我從$ http.post方法去除MSGDATA,一切工作正常。 誰能告訴我在哪裏的問題是要不指導我如何JSON數據發送到網絡服務
感謝您的幫助
**Edited:
The Issue was with the CORS, Im using codeigniter REST Controller for web-services.
Modified the headers. If anyone has the same issue add the below header in the construct
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) {
die();
}
Thanks to Linial for the break-through, telling me where the issue is.**
您的webservice是否期望JSON或表單數據? (例如:「message = test&x = test2」) – 2014-11-08 15:50:18
webservice只期望JSON格式 – 2014-11-08 15:54:44