Json 使用http.post方法-XmlHttpQuest进行过帐无法加载服务
Angular$http.post方法未将JSON发布到服务(RESTFul服务、节点服务)。 显示以下错误:Json 使用http.post方法-XmlHttpQuest进行过帐无法加载服务,json,google-chrome,firefox,angularjs,xmlhttprequest,Json,Google Chrome,Firefox,Angularjs,Xmlhttprequest,Angular$http.post方法未将JSON发布到服务(RESTFul服务、节点服务)。 显示以下错误: XMLHttpRequest无法加载/some/service。无效的HTTP状态代码404 这是张贴的代码 $http({method:'POST', url:'/some/service/',data:{"key1":"val1","key2":"val2"}}).success(function(result){ alert(result); }); 同样的代码也适用于我的
XMLHttpRequest无法加载/some/service。无效的HTTP状态代码404
这是张贴的代码
$http({method:'POST', url:'/some/service/',data:{"key1":"val1","key2":"val2"}}).success(function(result){
alert(result);
});
同样的代码也适用于我的chrome旧版本,即v29…*。我将chrome更新为V30…*。现在,它不起作用了。在Firefox中也不起作用。chrome和Firefox有什么问题吗
有人能帮忙吗?好吧,几天前发布了一个新的chrome更新。如果他们更改了任何与安全相关的内容,请查看该版本中的补丁说明。
几天前,我的扩展在FF和Chrome中都停止了工作。在将Chrome更新到30.0.1599.101版本后,我遇到了一个类似的问题,结果证明是服务器问题 我的服务器是使用Express()实现的,下面的代码允许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
}
);
正如本文()所指出的,其目的是确保您的服务器正确处理选项请求,以便启用CORS 大家好,欢迎访问SO,请记住在发布时将所有示例代码放在代码块中。此调用是在同一个域上吗?我想你可能正试图调用另一个域,因此浏览器出于安全原因阻止了它。如果是这样的话,试试JSONP。