I want to read the JSONP data from an external domain in raw JavaScript (no jQuery).
So let's say https://www.domain.com/abc.php?foo=bar
contains: {"error":false,"data_a":"abcabc","data_b":"123-456"}
I couldn't really find much about this on google. But if I understood it correctly it should work about like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://www.domain.com/abc.php?foo=bar&callback=DataCallback";
</script>
</head>
<body>
<script>
function DataCallback(data) {
for (var key in data) {
var value = data[key];
alert(key+' = '+value);
}
}
</script>
</body>
</html>
I don't get an error, but I also don't get any alerts popping up.
Btw, I noticed that
https://www.domain.com/abc.php?foo=bar
and
https://www.domain.com/abc.php?foo=bar&callback=DataCallback
both show {"error":false,"data_a":"abcabc","data_b":"123-456"} in the browser.
Could that be the problem? Because I thought that the second link should show:
DataCallback({"error":false,"data_a":"abcabc","data_b":"123-456"})
?