I have the following code snippet:
var musica_atual = '';
var capasplit = '';
function checaURL(endereco){
var http = new XMLHttpRequest();
http.open('HEAD', endereco, false);
http.send();
return http.status != 404;
}
// Atualiza os nomes das musicas
function atualiza_nome_musica() {
$.ajax({
url : 'http://somdomato.com:8000/stats?sid=1&json=1',
dataType : 'jsonp',
timeout : '4000',
success: function(data) {
if (data && data.songtitle) {
musica_atual = data.songtitle || '';
capasplit = musica_atual.split('-');
// testa se tem locutor ao vivo
if (musica_atual.substr(0, 7) === 'Ao Vivo') {
$('#pedidos_ativos').hide();
$('#pedidos_desativados').show();
$('.aovivo').show();
$('.proximas').hide();
// busca foto do locutor
var aux = musica_atual.split('-');
if (aux[1]) {
var nome_locutor = $.trim(aux[1]);
var musica_limpa2 = musica_atual.substr(musica_atual.indexOf("-") + 1);
var musica_atual = musica_limpa2.substr(musica_limpa2.indexOf("-") + 1);
$('#foto_locutor').attr('src', 'img/locutores/'+nome_locutor+'.jpg').load(function() {
$(this).show();
});
}
} else {
$('.aovivo').hide();
$('.proximas').show();
$('#pedidos_ativos').show();
}
$('#nome_musica').html( musica_atual );
$('#nome_locutor').html( nome_locutor );
}
}
});
if (capasplit[0] != '') {
$.getJSON("https://ajax.googleapis.com/ajax/services/search/images?callback=?", {
q: '"' + capasplit[0] + '"',
v: '1.0'
}, function(data) {
capa_nova = data.responseData.results[0].url;
if (checaURL(capa_nova)) {
$('#capa_musica').html('<img src="' + data.responseData.results[0].url + '">');
} else {
$('#capa_musica').html('<img src="/img/player/violao.png">');
}
});
} else {
$('#capa_musica').html('<img src="/img/player/violao.png">');
}
}
atualiza_nome_musica();
setInterval(atualiza_nome_musica, 10000);
Everything works perfect, the problem is that getting a new cover every 10 seconds generates a certain LAG and some people have complained that the radio is stalling.
And the checaURL function is generating this error:
XMLHttpRequest cannot load http://www.cottagespot.com/images/no-image-large.png. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://somdomato.com' is therefore not allowed access.
How can I prevent this from happening?