What is wrong with requesting this script?

0

At some point I need to request a json script from the server, but I'm making some changes and I'm getting errors that supposedly should not happen. The example below works perfectly well:

$(".script_episodios").html($("<script />", { 
    src: 'http://dominio.com/js/episodios.php'
}));
/*chamados os dados que foram gerados no script acima*/
console.log(dados_episodios);

asked by anonymous 19.04.2017 / 14:44

1 answer

0

The technique of creating script tag with src only works for JSONP, if the url returns JSON instead of JSONP you will have to do a normal HTTP request using jQuery.ajax() or XMLHttpRequest or fetch() . This request may not work if the server is not CORS enabled, if it is not with cors you can use a proxy like crossorigin.me.

Example using XMLHttpRequest

var request = new XMLHttpRequest();
request.open('GET', 'https://crossorigin.me/http://api.bluanime.com/dados/animes/113/episodios');
request.addEventListener('load', function () {
  var data = request.response.slice("dados_eps = ".length);
  alert(data);
  var json = JSON.parse(data);
});
request.send();

Example using JSONP by changing API: link

The api returns something like

callback({"episodes": "123", "_": "1492622616910"});
    
19.04.2017 / 15:19