Javascript 客户端到服务器的GET请求给出错误号';访问控制允许原点';
我正在本地主机上运行node/express js应用程序。我正在向Instagram的api发出“获取”请求,并不断收到以下错误:Javascript 客户端到服务器的GET请求给出错误号';访问控制允许原点';,javascript,jquery,node.js,express,instagram-api,Javascript,Jquery,Node.js,Express,Instagram Api,我正在本地主机上运行node/express js应用程序。我正在向Instagram的api发出“获取”请求,并不断收到以下错误: XMLHttpRequest cannot load https://api.instagram.com/oauth/authorize/?client_id=******&redirect_uri=http://localhost:4000/feed&response_type=code. No 'Access-Control-Allow-Or
XMLHttpRequest cannot load https://api.instagram.com/oauth/authorize/?client_id=******&redirect_uri=http://localhost:4000/feed&response_type=code.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4000' is therefore not allowed access.
我在我的服务器中发出如下请求:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Headers: x-requested-with");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/',function(req,res){
res.redirect(redirected to feed route);
})
app.get('/feed',function(req,response) {
var code = req.query.code;
var url = "https://api.instagram.com/oauth/access_token";
var options = {
url: url,
method: "POST",
form: {
client_id : clientId,
client_secret : client_secret,
grant_type: 'authorization_code',
redirect_uri: redirect_uri,
code: code
},
json: true
}
request(options, function(err,res,body){
var instagram_response = body;
response.json({access_info:instagram_response});
})
})
通过访问服务器路由中的“/”获取数据可以正常工作。当我在客户端(当Gallery.html加载时)使用下面的jQuery调用服务器“/”路由时,它会给出错误。下面是在客户端加载gallery.html时运行的函数
$(function(){
$.get("/", function( instagram_reponse, access_token) {
access_token = instagram_reponse.access_token;
})
})
我收到此错误是因为我的节点服务器正在本地主机上运行吗?我做错了什么?您需要在节点上安装CORS包
$ npm install cors
var express=require('express')
,cors=require('cors'))
,app=express()
配置选项
origin:配置访问控制允许originCORS头。需要一个字符串(例如:“”)。设置为true以反映请求来源,如req.header(“来源”)所定义。设置为false以禁用CORS。也可以设置为一个函数,该函数将请求源作为第一个参数,回调(需要签名err[object],allow[bool])作为第二个参数。最后,它还可以是正则表达式(/example.com$/)或要匹配的正则表达式和/或字符串数组
方法:配置访问控制允许方法CORS头。需要逗号分隔的字符串(例如:“GET、PUT、POST”)或数组(例如:[“GET”、“PUT”、“POST]”)
allowedHeaders:配置访问控制允许标头CORS标头。需要逗号分隔的字符串(例如:“内容类型,授权”)或数组(例如:[“内容类型”,“授权”)。如果未指定,则默认反映请求的访问控制请求标头中指定的标头
exposedHeaders:配置访问控制Exposed Headers CORS header。需要逗号分隔的字符串(例如:“Content-Range,X-Content-Range”)或数组(例如:[“Content-Range”,“X-Content-Range]”)。如果未指定,则不显示自定义标头。
凭据:配置访问控制允许凭据CORS标头。设置为true以传递标头,否则将忽略它
最大年龄:配置访问控制允许最大年龄CORS标头。设置为整数以传递标头,否则将忽略它
飞行前继续:将CORS飞行前响应传递给下一个处理程序
有关更多详细信息,请参阅此您没有所需的所有CORS标题。这有一些软件包。谷歌那个错误。。。这一问题每天都会被问到很多次,有很多资源可以解释。参见我添加的COR,如:
var express=require('express');var cors=要求(“cors”);var请求=要求(“请求”);var-app=express();应用程序使用(cors());app.get('/products/:id',function(req,res,next){res.json({msg:'这是所有源代码都启用的CORS!'});})代码>但我仍然得到一个错误。我不明白/products/:id
的用法。这并不能在任何地方解释它。我应该照着你给我的复制粘贴吗?
app.use(cors());
app.get('/products/:id', function(req, res, next){
res.json({msg: 'This is CORS-enabled for all origins!'});
});
app.listen(80, function(){
console.log('CORS-enabled web server listening on port 80');
});