PDA

View Full Version : call a json file with a value selection



Dasu-ku
09-12-2012, 12:11 PM
Hi everybody, i'm a new user of webGl and while i was working i found this interesting and tried to do but it doesn't work.

well this is my question:

I have this part of code i think that the calling is correct, the first time i load the page it uses to appear the image selected by default but when i select the other image this doesn't appear, besides the default image never change (disappear or something so ) that makes me think that the call is not even working.

Maybe i have to create another method or something like that? another kind of call? or i am forgetting something?

in my list i have two options exactly 1 and 2.
i based on the calling for textures that works well.

function x() {
var obj = document.getElementById("objs").value;

if (obj == "1") {
var request = new XMLHttpRequest();
request.open("GET", "obj1.json");
request.onreadystatechange = function () {
if (request.readyState == 4) {
handleLoadedTeapot(JSON.parse(request.responseText ));
}
}


request.send();

} else if (obj == "2") {
var request = new XMLHttpRequest();
request.open("GET", "obj2.json");
request.onreadystatechange = function () {
if (request.readyState == 4) {
handleLoadedTeapot(JSON.parse(request.responseText ));
}
}
request.send();
}
}


If you know another method for calling different json files in the same window or give me an idea of what should i do i would thank you.
bye.

cgardner5
09-15-2012, 11:07 AM
This is from emoller webgl-utilities located https://github.com/emoller/WebGL101/blo ... l-utils.js (https://github.com/emoller/WebGL101/blob/master/webgl-utils.js)
He has many webgl code resources.

function loadFile(file, callback, noCache, isJson) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 1) {
if (isJson) {
request.overrideMimeType('application/json');
}
request.send();
} else if (request.readyState == 4) {
if (request.status == 200) {
callback(request.responseText);
} else if (request.status == 404) {
throw 'File "' + file + '" does not exist.';
} else {
throw 'XHR error ' + request.status + '.';
}
}
};
var url = file;
if (noCache) {
url += '?' + (new Date()).getTime();
}
request.open('GET', url, true);
}