What is wrong with the request for this script?

Asked

Viewed 215 times

0

At a certain point I need to request a json script from the server, but I’m making some changes and I’m getting errors that supposedly shouldn’t 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);

When I make the same call to another unique domain to return json, it is no longer processed correctly

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

In the second example it seems that the script does not wait for the call to be completed before continuing and I get the error Uncaught ReferenceError: dados_episodios is not defined, I have already consulted the return of both requests is equal, including the average time of the request is equal for both, how can I circumvent this error?

  • Both src are redirecting to the page: http://www.domain.com/. How can it be working there? :

  • One points to the local domain, the other points to the Ubdomain api

  • But this Domain.com domain does not lead to a file json. Used only as an example???

  • Based on parameters passed PHP searches the data and sends them back with json_encode

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 make 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 Cors enabled you can use a proxy such as 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 the API: https://jsfiddle.net/Lmqgrwdt/

The api returns something like

callback({"episodes": "123", "_": "1492622616910"});
  • But the page and the parameters passed are the same, it only changes that now the request is made to a subdomain and not to the main domain. The same is sent, processed and get a return, so I don’t think it’s a problem with CORS.

  • Missing to see if this other URL returns JSON or JSONP, if returning only JSON will not work.

  • Here is an example of a return to the subdomain http://api.bluanime.com/dados/animes/113/episodios

  • Try to make setTimeout(() => console.log(dados_eps), 5000);, sometimes it takes a while to load.

  • But this is not applicable on a website, force loading wait 5 seconds before continuing execution, would be very impracticable

  • Just to see if it works. If it works you can put this script tag in html, after the load the variable will be ready. Another possibility is to try ajax.

  • Unfortunately you can not load it together with HTML because it is a dynamic page, whose content is changed via ajax, so every time the user makes any changes I need to load this script again.

  • Want to try using ajax? You’ll have to cut the beginning of the string with var x = {} to get only json.

  • I accept the suggestion as long as there is no problem with the response time

  • It has CORS disabled so I had to use the proxy crossorigin.me. It would be nice if people did fun({...}) instead of var x = {...}; because it would avoid using the proxy.

  • I can change the return of json is no problem, as it would be if I just sent fun({...})?

  • I put the example using JSONP, instead of setting a variable the returned code calls a previously defined function.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.