That part is pretty weird. It’ll turn into false, but was it intended to mean something else? What is it that generated this request in the first place? (The usual convention for “data not present” BTW is null, not false)
It would involve either writing the file as a JS file to the document, or carefully parsing the blob of text, replacing the JS code and turning it into valid JSON, then running JSON.parse on the resultant string (latter is what I tried to do when checking what you were trying to do).
It isn’t executable JS, it’s just a text file. You can’t fetch random text files then execute them, that’s not something that’s a great idea security-wise.
If it was JSON data, this would be no problem, because you then just run JSON.parse and get the data as an object. But it is not.
The code I’m using to check this:
async function f() {
const res = await fetch("https://api.github.com/gists/0d0229296e72aa8b42081729de2e2509");
const data = await res.json();
// We're fine at this point, when I do
// this I get the file contents:
console.log(data.files["sample-data.js"].content);
// BUT IT IS JUST TEXT
try {
let fileText = data.files["sample-data.js"].content;
// NOW TRYING TO MAKE IT INTO
// SOMETHING THAT WILL GO INTO
// THE JSON PARSE FUNCTION
// WITHOUT EXPLODING
fileText = fileText.replace(/XG = /, "");
fileText = fileText.replace(/id: /gm, "\"id\": ");
fileText = fileText.replace(/name: /gm, "\"name\": ");
fileText = fileText.replace(/hash: /gm, "\"hash\": ");
fileText = fileText.replace(/number: /gm, "\"number\": ");
fileText = fileText.replace(/email: /gm, "\"email\": ");
fileText = fileText.replace(/address: /gm, "\"address\": ");
fileText = fileText.replace(/;/gm, "");
// I gave up at this point as I have
// better things to do, but this fails:
return JSON.parse(fileText);
} catch (err) {
console.error(err)
}
}
Just fork the gist and convert it to JSON, then you can actually get JSON and parse it. Converting arbitrary text (and yes, even though this text has the title sample-data.js it’s still just arbitrary text, it’s no use for transferring over a network) to JS is not a good idea
Would switching from AJAX to fetch cause problems within a program or lose browser compatibility with certain things? I know fetch is used to get out of callback hell, but it seems like it lacks the browser compatibility for IE that AJAX has.
No, problem is that you need to replace all of the keys as well (name needs to be "name") for example, + get rid of the ; at the end, + get rid of the !1's.
Edit: and stringify was blowing up on me, I think because it was a string to start with that iot couldn’t parse as being JS but maybe I was just doing something bad
So I have this monstrosity:
async function f() {
const res = await fetch("https://api.github.com/gists/0d0229296e72aa8b42081729de2e2509");
const data = await res.json();
// We're fine at this point, when I do
// this I get the file contents:
console.log(data.files["sample-data.js"].content);
// BUT IT IS JUST TEXT
try {
let fileText = data.files["sample-data.js"].content;
// manually parse the JS code string into something JSON-parse-compatible
fileText = fileText.replace(/XG = /, "");
fileText = fileText.replace(/id: /gm, "\"id\": ");
fileText = fileText.replace(/name: /gm, "\"name\": ");
fileText = fileText.replace(/hash: /gm, "\"hash\": ");
fileText = fileText.replace(/number: /gm, "\"number\": ");
fileText = fileText.replace(/email: /gm, "\"email\": ");
fileText = fileText.replace(/address: /gm, "\"address\": ");
fileText = fileText.replace(/\!1/gm, "null");
fileText = fileText.replace(/;/gm, "");
const obj = JSON.parse(fileText);
console.log(obj)
return obj;
} catch (err) {
console.error(err)
}
}
Yeah, just edited the reply, I was missing the !1 replacement which is why I couldn’t get it to go through stringify & why I ended up manually replacing everything
I mean, the general consumer operating systems IE runs on are not supported by Microsoft anymore so it’s a small concern unless you have very specific needs (Windows Server support, for example), but yeah it can be polyfilled very easily if that support is needed.