This is a follow up to this question: JavaScript XMLHttpRequest using JsonP
I was trying to experiment with JsonP, since I'm trying to find a replacement for using a Cors proxy. I tried the example listed in the 2nd answer of the linked question but I got this error:
SyntaxError: expected expression, got '<'[Learn More] www.google.com:1
Why is that?
The code used:
function jsonp(url) {
return new Promise(function(resolve, reject) {
let script = document.createElement('script')
const name = "_jsonp_" + Math.round(100000 * Math.random());
//url formatting
if (url.match(/\?/)) url += "&callback="+name
else url += "?callback="+name
script.src = url;
window[name] = function(data) {
resolve(data);
document.body.removeChild(script);
delete window[name];
}
document.body.appendChild(script);
});
}
var data = jsonp("https://www.google.com");
data.then((res) => {
console.log(res);
});