Answer the question
In order to leave comments, you need to log in
Problem when getting JSON from another site using script tag, how to solve correctly?
Loading JSON with javascript like this:
var script = document.createElement('script');
script.src = json_src;
document.body.appendChild(script);
script.onload = function() {
alert("Done!");
console.log(JSON.parse(script).innerHTML);
}
script.type = "application/json";
var script = document.createElement('script');
script.type = "application/json";
script.src = json_src;
document.body.appendChild(script);
script.onload = function() {
alert("Done!");
console.log(JSON.parse(script).innerHTML);
}
Answer the question
In order to leave comments, you need to log in
Calling JSON.parse(str) will turn a string of JSON data into a JavaScript object/array/value.
What are you doing? You are passing in JSON.parse the object created by document.createElement('script');
Replace
JSON.parse(script).innerHTML
with
JSON.parse(script.innerHTML) Do you
understand why?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question