Posting with the angular http.post method - xmlhttprquest cannot load the service

Angular $ http.post method does not send JSON for maintenance (RESTFul service, node). Display the following error:

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

Here is the published code

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

The same code works with the old version of my chrome ie, v29 ... *. I upgraded my chrome to V30 ... *. Now it does not work. It does not work in Firefox. Is there a problem with Chrome and Firefox?

Does anyone help?

+4
source share
2 answers

I encountered a similar problem after updating Chrome to version 30.0.1599.101, and this turned out to be a server problem.

Express (http://expressjs.com/) CORS ( CORS?) :

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}));
});

HTTP- :

$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
    }
);

(fooobar.com/questions/16817/...), , OPTIONS, CORS.

+2

, chrome . , - . FF, Chrome .

0

All Articles