Làm thế nào để gửi dữ liệu JSON để web phục vụ qua AngularJS đây là đoạn mãLàm thế nào để gửi dữ liệu JSON để REST Webservice trong 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: /// sứ/api/bài viết/savePost
ionic.bundle.js: 16.185 (chức năng ẩn danh) ionic.bundle.js: 16.185 sendReq ionic.bundle.js: 15.979 ionic.bundle.js serverRequest: 15.712 ionic.bundle.js wrappedCallback: 19.197 wrappedCallback ionic.bundle.js: 19197 (chức năng ẩn danh) ionic.bundle.js: 19283 $ Eval ionic.bundle.js: 20326 Phạm vi. $ Digest ionic.bundle.js: 20138 Phạm vi. $ Áp dụng ionic.bundle.js: 20430 (chức năng ẩn danh) ionic.bundle.js: 43025 (chức năng ẩn danh) ionic.bundle.js: 10478 forEach ionic.bundle.js: 7950 eventHandler ionic.bundle.js: 10477 triggerMouseEvent ionic.bundle.js: 2648 tapClick ionic.bundle.js: 2637 tapMouseUp ionic.bundle.js: 2707XMLHttpRequest không thể tải http: /// messenger/api/posts/savePost. Mã trạng thái HTTP không hợp lệ 404
Nhưng khi tôi xóa msgdata khỏi phương thức $ http.post, mọi thứ đều hoạt động tốt. bất cứ ai có thể cho tôi biết nơi vấn đề là nếu không hướng dẫn cho tôi làm thế nào để gửi dữ liệu JSON để web-dịch vụ
Thanks for the help
**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.**
Dịch vụ web của bạn có mong đợi JSON hoặc dữ liệu biểu mẫu không? (ví dụ: "message = test & x = test2") –
webservice chỉ mong định dạng JSON –