json - Posting with angular http.post method - xmlhttprquest cannot load the service -
angular $http.post method not posting json service (restful service, node service). showing following error :
xmlhttprequest cannot load /some/service. invalid http status code 404
here posted code
$http({method:'post', url:'/some/service/',data:{"key1":"val1","key2":"val2"}}).success(function(result){ alert(result); });
the same code working old version of chrome i.e, v29...* . updated chrome v30...* . now, not working. not working in firefox well. there problem chrome , firefox?
can help?
i came across similar issue after updating chrome version 30.0.1599.101 , turned out server problem.
my server implemented using express (http://expressjs.com/) , code below allowing cors (how allow cors?) works well:
var express = require("express"); var server = express(); var allowcrossdomain = function(req, res, next) { res.header('access-control-allow-origin', req.headers.origin || "*"); res.header('access-control-allow-methods', 'get,post,put,head,delete,options'); res.header('access-control-allow-headers', 'content-type,x-requested-with'); next(); } server.configure(function () { server.use(allowcrossdomain); }); server.options('/*', function(req, res){ res.header('access-control-allow-origin', req.headers.origin || "*"); res.header('access-control-allow-methods', 'get,post,put,head,delete,options'); res.header('access-control-allow-headers', 'content-type,x-requested-with'); res.send(200); }); server.post('/some_service', function (req, res) { res.header('access-control-allow-origin', req.headers.origin); // stuff here //example of json response res.contenttype('json'); res.send(json.stringify({ok: true})); });
the http request looks like:
$http({ method: 'post', url: 'http://localhost/some_service', data: json.stringify({ key1: "val1", key2: "val2" }), headers: { 'content-type': 'application/json; charset=utf-8' } }).success( function (data, status, headers, config) { //do } ).error( function (data, status, headers, config) { //do } );
as pointed out in here (https://stackoverflow.com/a/8572637/772020), idea ensure server handles options request in order enable cors.
Comments
Post a Comment