POST AJAX请求被拒绝 - CORS? [英] POST AJAX request denied - CORS?

查看:943
本文介绍了POST AJAX请求被拒绝 - CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的node.js服务器端口5000上设置CORS如下:

  var app = express 

app.use(cors()); // normal CORS
app.options('*',cors()); // preflight

app.post('/ connect',function(req,res){
console.log(Connection request received!)
});

var port = 5000;
app.listen(port,function(){
console.log('Listening on port'+ port);
});

我现在尝试使用JQuery发送AJAX POST请求,在静态网页打开我的硬碟:

  var xhr = $ .post({
url:'http:// localhost:5000 / connect',
complete:function(){
console.log(done!)
},
crossDomain:true
}

xhr.fail(function(xhr,status,error){
alert(error)
})
$ p>

完成函数从未被调用,并且我获得的唯一警报是来自XHR fail 处理程序,包含以下错误:

  NS_ERROR_DOM_BAD_URI:访问受限URI拒绝

我认为CORS配置良好,我缺少什么? >




编辑:对于我的案例,我能找到的最好的解决方案是发送页面服务器:

  app.use(express.static('web'))

应用程序。 get(/,function(req,res){
res.sendFile(__ dirname +'/web/HTML/index.html');
});


解决方案

这是我使用的代码。它位于我的app.js文件中

  app.all(/ *,function(req,res,next) {

res.header(Access-Control-Allow-Origin,req.headers.origin);
res.header(Access-Control-Allow-Credentials,true) ;
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS');
res.header '
if('Content-Type,Accept,X-Access-Token,X-Key,Authorization,X-Requested-With,Origin,Access-Control-Allow-Origin,Access-Control-Allow-Credentials' (req.method ==='OPTIONS'){
res.status(200).end();
} else {
next();
}
});


I have setup CORS on my node.js server on port 5000 as follows:

var app = express();

app.use(cors()); //normal CORS
app.options('*', cors()); //preflight 

app.post('/connect', function(req, res) { 
    console.log("Connection request received !")
});

var port = 5000;
app.listen(port, function () {
    console.log('Listening on port '+port);
});

I am now trying to send AJAX POST requests using JQuery like so, in a static web page open from my hard disk :

var xhr = $.post({
            url: 'http://localhost:5000/connect',
            complete: function() {
                console.log("done !")
            },
            crossDomain: true
        });

xhr.fail(function(xhr, status, error){
    alert(error)
})

The complete function is never called, and the only alert I get is an alert from the XHR fail handler, containing the following error :

NS_ERROR_DOM_BAD_URI: Access to restricted URI denied

I think CORS is well-configured, what am I missing ?


EDIT : for those in my case, the best solution I could find was to send the page from the server :

app.use(express.static('web'))

app.get("/", function(req, res) {
    res.sendFile(__dirname + '/web/HTML/index.html');
});

解决方案

This is the code I am using. It is located in my app.js file

app.all("/*", function (req, res, next) {

  res.header("Access-Control-Allow-Origin", req.headers.origin);
  res.header("Access-Control-Allow-Credentials",true);
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type,Accept,X-Access-Token,X-Key,Authorization,X-Requested-With,Origin,Access-Control-Allow-Origin,Access-Control-Allow-Credentials');
  if (req.method === 'OPTIONS') {
    res.status(200).end();
  } else {
    next();
  }
});

这篇关于POST AJAX请求被拒绝 - CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆