Preferisco usare jjery ajax. Jquery rende molto più facile vivere.
Quello che per esempio può fare sul server lato è, suppongo che voi state usando PHP:
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
// if it's an ajax request
$json['success'] = 1;
$json['html'] = '<div id="test">..[more html code here].. </div>';
echo json_encode($json);
}else{
// if it's an non ajax request
}
Sul lato client è possibile eseguire le seguenti operazioni utilizzando jQuery Ajax:
$.ajax({
type: "POST",
url: "[your request url here]",
data: { name: "JOKOOOOW OOWNOOO" },
complete: function(e, xhr, settings){
switch(e.status){
case 500:
alert('500 internal server error!');
break;
case 404:
alert('404 Page not found!');
break;
case 401:
alert('401 unauthorized access');
break;
}
}
}).done(function(data) {
var obj = jQuery.parseJSON(data)
if (obj.success == 1){
$('div#insert_html_div').html(obj.html);
}else if (obj.error == 1){
}
// etc
});
jQuery rende Ajax più facile, così come molte altre librerie, ma puoi fare Ajax con Vanilla JS. – nnnnnn
Hai un file statico sul tuo server web che contiene dati JSON serializzati e vuoi leggere quei dati usando AJAX? –
sì, Mike Christensen – rubo77