我正在尝试在node.js中构建一个支持跨域脚本的Web服务器,同时仍然提供来自公共目录的静态文件.我正在使用express.js,我不确定如何允许跨域脚本(Access-Control-Allow-Origin: *).
我看到这篇文章,我觉得没有用.
var express = require('express')
, app = express.createServer();
app.get('/', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.configure(function () {
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
});
app.configure('development', function () {
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
var oneYear = 31557600000;
// app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler());
});
app.listen(8888);
console.log('express running at http://localhost:%d', 8888);
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取新闻网站的Feed.我以为我会使用google的feed API将feedburner Feed转换为json.以下网址将以json格式从Feed返回10个帖子.http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/mathrubhumi
我使用以下代码来获取上面的url的内容
$.ajax({
type: "GET",
dataType: "jsonp",
url: "http://ajax.googleapis.com/ajax/services/feed/load",
data: {
"v": "1.0",
"num": "10",
"q": "http://feeds.feedburner.com/mathrubhumi"
},
success: function(result) {
//.....
}
});
Run Code Online (Sandbox Code Playgroud)
但它没有工作,我收到以下错误
XMLHttpRequest无法加载 http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http%3A%2F%2Ffeeds.feedburner.com%2Fmathrubhumi.请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" :HTTP //本地主机,因此"是不允许访问.
我该如何解决?
我正在使用Reactjs并通过javascript中的AJAX使用API。我们如何解决这个问题?以前我使用CORS工具,但现在需要启用CORS。
我想知道如何在 reactJS 或 react-native 环境的 axios post 方法中设置 Access-Control-Allow-Origin?我使用 CORS 附加组件,它可以工作,但我也想在标题中设置它,我尝试了这些方法,但没有一个不起作用。
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
Run Code Online (Sandbox Code Playgroud)
和
let axiosConfig = {
headers: {
'method':'POST',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
}
};
Run Code Online (Sandbox Code Playgroud) ajax ×3
cors ×3
javascript ×3
reactjs ×2
axios ×1
cross-domain ×1
express ×1
jquery ×1
json ×1
node.js ×1
react-native ×1
webserver ×1