Angular $http.post method is not posting JSON to service (RESTFul service, node service). Showing the following error :
Angular $ http.post方法未将JSON发布到服务(RESTFul服务,节点服务)。显示以下错误:
XMLHttpRequest cannot load /some/service. Invalid HTTP status code 404
XMLHttpRequest无法加载/ some / service。无效的HTTP状态代码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?
相同的代码正在使用旧版本的chrome,即v29 ... *。我将我的chrome更新为V30 ...... *。现在,它没有用。也不在Firefox中工作。 Chrome和Firefox有什么问题吗?
Can anybody help?
有人可以帮忙吗?
2 个解决方案
#1
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.
在将Chrome更新到版本30.0.1599.101后,我遇到了类似的问题,结果证明是服务器问题。
My server is implemented using Express (http://expressjs.com/) and the code below allowing CORS (How to allow CORS?) works well:
我的服务器使用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}));
});
The HTTP request looks like:
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
}
);
As pointed out in here (https://*.com/a/8572637/772020), the idea is to ensure that your server handles properly the OPTIONS request in order to enable CORS.
正如这里所指出的那样(https://*.com/a/8572637/772020),我们的想法是确保您的服务器正确处理OPTIONS请求以启用CORS。
#2
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.
好吧,几天前发布了一个新的Chrome更新。如果他们更改了与安全性相关的任何内容,请检查该发行版中几天前,我的扩展程序停止了在FF和Chrome中的工作。
#1
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.
在将Chrome更新到版本30.0.1599.101后,我遇到了类似的问题,结果证明是服务器问题。
My server is implemented using Express (http://expressjs.com/) and the code below allowing CORS (How to allow CORS?) works well:
我的服务器使用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}));
});
The HTTP request looks like:
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
}
);
As pointed out in here (https://*.com/a/8572637/772020), the idea is to ensure that your server handles properly the OPTIONS request in order to enable CORS.
正如这里所指出的那样(https://*.com/a/8572637/772020),我们的想法是确保您的服务器正确处理OPTIONS请求以启用CORS。
#2
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.
好吧,几天前发布了一个新的Chrome更新。如果他们更改了与安全性相关的任何内容,请检查该发行版中几天前,我的扩展程序停止了在FF和Chrome中的工作。