Ciao ragazzi, mi succede una cosa strana:
Provo a fare una chiamata ajax per farmi tornare un json da un asmx.
Questa chiamata se la faccio partire all'onclick di un pulsante va a buon fine. A me serve però che questa chiamata venga fatta appena siano pronti tutti gli elementi della pagina, quindi ho provato sia nel window load che nel document ready, ma a questo punto la chiamata non mi va ne a buon fine ne in errore. Non so cosa fare, avete suggerimenti?
Di seguito lo script:
codice:
<script type="text/javascript" language="javascript">
jQuery(document).ready(function ($) {
$.ajaxSetup({
cache: false,
dataType: "html",
error: function (xhr, status, error) {
alert('An error occurred: ' + error);
},
timeout: 30000, // Timeout of 30 seconds
type: "GET",
beforeSend: function () {
alert('In Ajax beforeSend function');
},
complete: function () {
alert('In Ajax complete function-Log message');
// console.debug('In Ajax complete function-Debug message');
// console.error('In Ajax complete function-Error message');
// console.info('In Ajax complete function-Info message');
// console.warn('In Ajax complete function-Warn message');
}
});
$(window).load(function () {
var id = $.urlParams('rif');
var valtipo = $.urlParams('type');
$.ajax({
type: 'POST',
url: "WebService.asmx/Get_Scheda",
data: JSON.stringify({ iddvm: "2362",
riferimento: id,
TipoTabella: valtipo
}),
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
beforeSend: function () {
// this is where we append a loading image
alert('prima');
},
success: function (data) //what to do if succedded
{
var obj = $.parseJSON(data.d);
alert('dentro');
$.each(obj, function () {
$('#latitudine').val(this['latitudine']);
$('#longitudine').val(this['longitudine']);
$("#riferimento").empty();
$("#riferimento").append("<span>Offerta " + id + "</span>
......................</p>");
$("#prezzo").empty();
$("#prezzo").append("<span class='price-tag'>" + this['prezzo'] + " EURO</span>");
$("#tipologia").empty();
$("#tipologia").append("<span class='value'>" + this['tipologia'] + "</span>");
$("#mq").empty();
$("#mq").append("<span class='value'>" + this['mq'] + "</span>");
$("#camere").empty();
$("#camere").append("<span class='value'>" + this['camere'] + "</span>");
$("#locali").empty();
$("#locali").append("<span class='value'>" + this['locali'] + "</span>");
$("#bagni").empty();
$("#bagni").append("<span class='value'>" + this['bagni'] + "</span>");
$("#posto_auto").empty();
$("#posto_auto").append("<span class='value'>" + this['posto_auto'] + "</span>");
$("#zona").empty();
$("#zona").append("<span class='value'>" + this['zona'] + "</span>");
$("#classe_energetica").empty();
$("#classe_energetica").append("<span class='value'>" + this['classe_energetica'] + "</span>");
if (this['foto1'] != '') {
$("#foto1").attr("src", this['foto1']);
//$("#afoto1").attr("href", this['foto1']);
}
if (this['foto1'] != '') {
$("#foto2").attr("src", this['foto1']);
$("#afoto2").attr("href", this['foto1']);
}
if (this['foto2'] != '') {
$("#foto3").attr("src", this['foto2']);
$("#afoto3").attr("href", this['foto2']);
}
if (this['foto3'] != '') {
$("#foto4").attr("src", this['foto3']);
$("#afoto4").attr("href", this['foto3']);
}
if (this['foto4'] != '') {
$("#foto5").attr("src", this['foto4']);
$("#afoto5").attr("href", this['foto4']);
}
if (this['foto5'] != '') {
$("#foto6").attr("src", this['foto5']);
$("#afoto6").attr("href", this['foto5']);
}
if (this['foto6'] != '') {
$("#foto7").attr("src", this['foto6']);
$("#afoto7").attr("href", this['foto6']);
}
$("#pertinenze").empty();
$("#pertinenze").append("Pertinenze: " + this['pertinenze']);
$("#impianti").empty();
$("#impianti").append("Impianti e Riscaldamento: " + this['impianti']);
$("#pavimenti").empty();
$("#pavimenti").append("Pavimenti e infissi: " + this['pavimenti']);
$("#accessori").empty();
$("#accessori").append("Accessori e Rifiniture: " + this['accessori']);
$("#servizi").empty();
$("#servizi").append("Servizi e Varie: " + this['servizi']);
$("#descrizione").empty();
$("#descrizione").append("
" + this['descrizione'] + "</p>");
$("#telefono").empty();
$("#telefono").append(this['telefono_referente']);
$("#indirizzo").empty();
$("#indirizzo").append(this['indirizzo_referente']);
$("#email").attr("href", "mailto:" + this['email_referente']);
$("#sito").empty();
$("#sito").append("" + this['sito_referente'] + "");
});
},
error: function (jqXHR, textStatus, errorThrown) //what to do if fails
{
alert('bad, ' + errorThrown + ", " + jqXHR.responseText + ", " + textStatus);
}
});
});
});
</script>