I know that there're lots of questions like this one and tat, but I really can't find where the problem is.
I need to get JSON from external url, using native Javascript.
I've done this:
loadUserData('http://ip-api.com/json', 'setUserData');
function loadUserData(url, callback)
{
var s = document.createElement('script');
s.src = url + '?callback=' + callback;
document.body.appendChild(s);
}
And when I'm getting this url, everything is okay, the callback is called, no errors, everyone's happy.
BUT, when I try to get json from my virtual host, I get Javascript error at the very start of this json
Unexpected token :
That json on my host contains big object, encoded with PHP's json_encode
, like
$data = new StdClass;
$data->id = $id;
$data->admin = 1;
$data->user = User::find(Auth::user()->id);
$data->websites = [1, 2, 3];
Storage::put('mysjon', json_encode($data));
And the most interesting part is that I tries to just copy string from ip-api and put it on my host (without any additional encoding, just like string, so in browser it looks just like the original string on ip-api website), and I've also got the same error. How does it happen that one and the same string can be easily loaded from one url and cause js error when loading from another?
Would highly appreciate any possible help!