I'am aware that there are many questions like this, but i wasnt able to solve my problem with the provided solutions. First of all i have a simple node.js webserver:
var serveFile = function(path, response) {
fs.readFile(__dirname + path, function(err, data) {
if (err) {
console.log('There is no file: ' + path);
response.end('Error ' + path);
} else {
if (path.endsWith('.png')) {
response.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
response.write(new Buffer(data).toString('base64'));
response.end();
console.log("Image Found");
} else {
response.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
response.write(data);
response.end();
console.log("File Found");
}
}
});
}
i use the serveFile
method to serve either .png
or .glsl
.
ok so far so good, when i request a .glsl
ressources, f.e. http://localhost:8080/src/engine/glsl/shader.glsl
:
xmlReq = new XMLHttpRequest();
console.log(filePath);
xmlReq.open('GET', 'http://localhost:8080/' + somePath, false);
try {
xmlReq.send();
} catch (error) {
alert('Error loading shader: ' + filePath);
return null;
}
shaderSource = xmlReq.responseText;
everything works fine, except that i should do it async :p.
BUT when i request the .png
with almost the exact same code:
function loadImage(file) {
console.log('loading....');
var xhr = new XMLHttpRequest();
xhr.open("GET", 'http://localhost:8080/'+ file, false);
try {
xhr.send();
} catch (error) {
console.error(error);
}
var dataURL = "data:image/png;base64," + this.responseText;
return dataURL;
}
I get the following error:
testGL.js:142 XMLHttpRequest cannot load http://localhost:8080/test/src_test/texture.png. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
I dont understand how can the .glsl
request work and the request for the image doesnt??? What am i missing? and how could i fix it?