ajax put 跨域,ajax put请求的跨域问题
为什么下面的post没问题,但是put请求会报错:XMLHttpRequest cannot load http://localhost:8001/service. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present o
为什么下面的post没问题,但是put请求会报错:
XMLHttpRequest cannot load http://localhost:8001/service. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63343' is therefore not allowed access.
客户端
$.ajax({
url: 'http://localhost:8001/man',
method: 'POST',
success: function (data) {
console.log(data)
}
});
$.ajax({
url: 'http://localhost:8001/service',
method: 'PUT',
success: function (data) {
console.log(data)
}
});
服务端
app.post('/man', function (req, res) {
var body = req.body;
console.log(body);
if (body) {
res.header("Content-Type", "application/json; charset=utf-8");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
res.send(body);
}
});
app.put('/service', function (req, res) {
res.header("Content-Type", "application/json; charset=utf-8");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
res.send(req.body);
});
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)