[CORS]Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource
本问题已经有最佳答案,请猛点这里访问。
我知道有很多解释,但它仍然无法解决我目前的问题。
基本上,我使用
XMLHttpRequest cannot load file:///sth. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | //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); }); }); |
I did include the header in my server
这里没有涉及服务器。
您正在从本地文件加载HTML文档。 您正在使用另一个本地文件的相对URL。
您应该通过