Signalwerk - Web-Code-Library

jQuery ajax, Standard AJAX http_request

jQuery ajax, einfaches Beispiel

$.ajax({type:'POST',url:'some.php',data:{name:'John',location:'Boston'}}).done(function(msg) {
alert(msg);
});


jQuery ajax, Daten eines Formulars versenden

$('#ajaxform').submit(function() {
$.ajax({type:'POST',url:'post.php',data:$('#ajaxform').serialize()}).done(function(msg) {
$('#ajaxform').html('Daten gespeichert' + msg);
});
return false;
})


Standard-AJAX-Aufruf plus Objekt mit responseText füllen

var http_request = false;
var my_ajax_container = false;
function ajx(myurl,mycontainer) {
http_request = false;
if (window.XMLHttpRequest) { http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); } }
else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
if (http_request) { my_ajax_container = mycontainer; http_request.onreadystatechange = ajax_fill_container; http_request.open("GET",myurl,true); http_request.send(null); }
else { alert('Fehler: Kann keine XMLHTTP-Instanz erzeugen'); }
}
function ajax_fill_container() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
if (http_request.responseText.length > 0) {
var mycontent = http_request.responseText;
document.getElementById(my_ajax_container).innerHTML = mycontent;
}
}
else { alert('Es ist ein Fehler aufgetreten'); }
}
}


Funktion aufrufen

var q = '/ajax.php?c1=asdfjklö';
ajx(encodeURI(q),'my_div');


http://www.webmasterpro.de/coding/article/ajax-hello-world-in-ajax-inhalt-dynamisch-nachladen.html

Zurück zur Web-Code-Lib