Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery XMLHttpRequest,请求的资源中没有访问控制允许来源_Jquery_Ajax_Node.js - Fatal编程技术网

Jquery XMLHttpRequest,请求的资源中没有访问控制允许来源

Jquery XMLHttpRequest,请求的资源中没有访问控制允许来源,jquery,ajax,node.js,Jquery,Ajax,Node.js,面对下面的问题,我正在访问另一台服务器上的一个文件,它是7777的3000。平台是node.js XMLHttpRequest cannot load http://localhost:3000/register_user. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7777' is therefore not allowed acc

面对下面的问题,我正在访问另一台服务器上的一个文件,它是7777的3000。平台是node.js

XMLHttpRequest cannot load http://localhost:3000/register_user. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7777' is therefore not allowed access.

提前谢谢

您可以通过添加访问控制允许源代码,通过HTTP头来控制这一点。将其设置为
访问控制允许来源:
将接受来自任何域的跨域AJAX请求

尝试将其添加到app.js文件中

var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');  

    next();
};
然后像这样使用:-

app.use(allowCrossDomain);

我只在一个领域需要它

var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', req.headers.orgigin);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');  

    next();
};
app.use(allowCrossDomain);

node.js脚本中不允许跨域请求。谢谢。它正在工作,但什么是“p3p”。最后两个iines@DineshRawat如果答案有帮助,你应该接受它,这样可以帮助其他用户