4

Angular $http.post method is not posting JSON to service (RESTFul service, node service). Showing the following error :

XMLHttpRequest cannot load /some/service. Invalid HTTP status code 404

Here is the posted code

$http({method:'POST', url:'/some/service/',data:{"key1":"val1","key2":"val2"}}).success(function(result){
  alert(result);
});

The same code is working with the old version of my chrome i.e, v29...* . I updated my chrome to V30...* . Now, it is not working. Not working in the Firefox as well. Is there any problem with chrome and Firefox?

Can anybody help?

2
  • Hi there, welcome to SO, please remeber to put all example code within code blocks when posting. Commented Oct 6, 2013 at 17:44
  • Is this call on the same domain? I think you may be trying to call another domain so the browsers block it for security reasons. Try JSONP if thats the case. Commented Oct 6, 2013 at 21:21

2 Answers 2

2

I came across a similar issue after updating Chrome to version 30.0.1599.101 and it turned out to be a server problem.

My server is implemented using Express (http://expressjs.com/) and the code below allowing CORS (How to 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 a 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 something
    }
).error(
    function (data, status, headers, config) {
        //do something
    }
);

As pointed out in here (https://stackoverflow.com/a/8572637/772020), the idea is to ensure that your server handles properly the OPTIONS request in order to enable CORS.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, a new chrome update was released a couple of days ago. Check the patch notes from that release if they changed anything security related. My extension stopped working both in FF and Chrome a couple of days ago.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.