javascript [CORS]跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https、chrome-extension-resource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33031653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[CORS]Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource
提问by Someone
I know there are so many explanations out there, but it still doesn't solve my current problem.
Basically, I was using /sth
and ``/sthElseto render into my index.html (which is
/`) because one is post another one is comments, I did include the header in my server, but but I still got an error says:
我知道有很多解释,但它仍然不能解决我目前的问题。基本上,我正在使用/sth
和 ``/sthElse to render into my index.html (which is
/`) 因为一个是发布另一个是评论,我确实在我的服务器中包含了标题,但是我仍然收到一个错误说:
XMLHttpRequest cannot load file:///sth. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource."
XMLHttpRequest 无法加载 file:///sth。跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https、chrome-extension-resource。”
//client jquery ajax
function something(){
$.ajax({
method: 'GET',
url: '/sth',
headers: headers,
success: function(data) {
console.log(data);
}
});
}
function somethingElse(){
$.ajax({
method: 'GET',
url: '/sthElse',
success: function(data) {
console.log('comment',data);
}
}
}
});
}
// server express
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'example.com');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get('/sth', function(request, response){
var req = http.get(url+"posts", function(res){
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
var bodyChunks = [];
res.on('data', function(chunk) {
bodyChunks.push(chunk);
})
.on('end', function() {
var body = Buffer.concat(bodyChunks);
response.send(JSON.parse(body));
})
});
req.on('error', function(e) {
console.log('ERROR');
});
});
app.get('/sthElse', function(request, response){
console.log('STATUSasdf: ' + response.statusCode);
var req = http.get(url+'comments', function(res) {
var bodyChunks = [];
res.on('data', function (chunk) {
bodyChunks.push(chunk);
})
.on('end', function() {
console.log('this is bodychunks pre decription', bodyChunks);
var body = Buffer.concat(bodyChunks);
response.send(JSON.parse(body));
})
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});
回答by Quentin
I did include the header in my server
我确实在我的服务器中包含了标题
There is no server involved here.
这里不涉及服务器。
You are loading the HTML document from a local file. You are using a relative URL to another local file.
您正在从本地文件加载 HTML 文档。您正在使用另一个本地文件的相对 URL。
You should load both files over http
, which should resolve the problem.
您应该通过 加载这两个文件http
,这应该可以解决问题。