Come inviare dati JSON per web-service tramite AngularJS Ecco il frammento di codiceCome inviare dati JSON per REST Webservice in 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);
});
}
})
OPZIONI http: /// messenger/API/messaggi/savePost
ionic.bundle.js: 16185 (funzione anonima) ionic.bundle.js: 16185 ionic.bundle.js sendReq: 15979 ionic.bundle.js serverRequest: 15712 ionic.bundle.js wrappedCallback: 19197 wrappedCallback ionic.bundle.js: 19197 (funzione anonima) ionic.bundle.js: 19283 Ambito. $ Eval ionic.bundle.js: 20326 Scope. $ Digest ionic.bundle.js: 20138 Scope. $ Apply ionic.bundle.js: 20430 (funzione anonima) ionic.bundle.js: 43025 (funzione anonima) 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 non può caricare http: /// messenger/api/posts/savePost. HTTP valido codice di stato 404
Ma quando rimuovo il MSGDATA da $ http.post metodo, tutto funziona bene. qualcuno può dirmi dove la questione è altrimenti mi guida come inviare dati JSON al web-service
Grazie per l'aiuto
**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.**
Il tuo webservice si aspetta dati JSON o del modulo? (es: "message = test & x = test2") –
webservice si aspetta solo il formato JSON –