私たちは主に Ajax を使用します。これは、ページを離れることなく、サーバー側のページを呼び出すクライアント側の Javascript コードで構成されています。
GETメソッド ( JSFiddle )を使用して、ページの表示コンテンツを取得する例を次に示します。
var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onreadystatechange = function(){
if(xhr.readyState==4 && ((xhr.status>=200 && xhr.status<300) || xhr.status==304)){//Checks if the content was loaded
console.log(this.responseText);
}
}
xhr.open('GET','myPHPPage.php?foo=foo&bar=bar',true);
xhr.send();
ここではPOSTメソッド ( JSFiddle ) を使用します。
var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
var data = 'foo=foo&bar=bar';
xhr.onreadystatechange = function(){
if(xhr.readyState==4 && ((xhr.status>=200 && xhr.status<300) || xhr.status==304)){//Checks if the content was loaded
console.log(this.responseText);
}
}
xhr.open('POST','myPHPPage.php',true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.setRequestHeader('Content-length',data.length);
xhr.send(data);
ここでは、setRequestHeader
メソッドを使用してこの HTTP 要求のヘッダーを変更し、この場合は と を変更してContent-type
いることに注意してくださいContent-length
(このヘッダーのデフォルト値は 4096 バイトです)。また、setRequestHeader
メソッドはメソッドの後に呼び出す必要open
があります。
これらのリンクはあなたを助けるはずです:
https://developer.mozilla.org/en/Ajax
http://code.google.com/intl/pt-BR/edu/ajax/tutorials/ajax-tutorial.html