AJAX サーバーリクエストのポストまたは取得
AJAX post または get サーバーリクエスト:
XMLHttpRequest オブジェクトは、サーバーとデータを交換するために使用されます。
サーバーにリクエストを送信したい場(chǎng)合は、XMLHttpRequest オブジェクトの open() メソッドと send() メソッドを使用する必要があります。
プロパティ | 説明 |
open(method,url,async) | リクエストのタイプ、URL、リクエストを処理するかどうかを指定します非同期的に。 (1).method: リクエストのタイプ。GET または POST。 (2).url: サーバー上のファイルの場(chǎng)所。 (3).async:true (非同期) または false (同期)。 |
send(string) | リクエストをサーバーに送信します。 文字列: POST リクエストにのみ使用されます |
1. get と post の違い:
get メソッドの方が高速で適用性が高い場(chǎng)合がありますが、多くの場(chǎng)合、依然として post を使用する必要があります。
post メソッドを使用するための推奨シナリオは次のとおりです:
(1) キャッシュされたファイルの使用 (サーバー上のファイルまたはデータベースの更新) を想定しないでください。
(2). 大量のデータをサーバーに送信します (POST にはデータ量制限はありません)。
(3). 不明な文字を含むユーザー入力を送信する場(chǎng)合、POST は GET よりも安定しており、信頼性が高くなります。
2. Get メソッド:
まず get メソッドのコードを見(jiàn)てみましょう:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "demo/ajax/net/demo.aspx", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
上記のコードで、ボタンをクリックしてサーバーの現(xiàn)在の日付と時(shí)刻を取得します。 C# コードは次のとおりです。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ajax { public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write(System.DateTime.Now); } } }
特記事項(xiàng): 上記 IE ブラウザはキャッシュからデータを読み取ることがあります。これは、ボタンの最初のクリックで時(shí)刻と日付が正常に取得された後、それ以降のクリックは効果がないことを意味します。 Google または Firefox の場(chǎng)合、解決策は次のとおりです:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://www.softwhy.com/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "demo/ajax/net/demo.aspx?rnd=" + Math.random(), true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
解決策は非常に簡(jiǎn)単で、URL の後に亂數(shù)を追加するだけです。これにより、各リクエストは新しい URL とみなされ、キャッシュされなくなります。
GET を使用して情報(bào)を送信することもできます。URL を通じて情報(bào)を送信することもできます。コードは次のとおりです:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "demo/ajax/net/demoPara.aspx?webName="+escape("php中文網(wǎng)")+"&age=3", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
ボタンをクリックして指定されたコンテンツを取得します。以下は C# コードです:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ajax { public partial class demoPara : System.Web.UI.Page { string webName; int age; protected void Page_Load(object sender, EventArgs e) { webName =Server.UrlDecode(Request.QueryString["webName"]); age = Convert.ToInt32(Request.QueryString["age"]); Response.Write("歡迎來(lái)到" + webName + ",本站已經(jīng)成立" + age + "周年。"); } } }
3 つ。 POST リクエスト:
コード例:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("POST", "demo/ajax/net/demo.aspx", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
上記のコードは、post メソッドを使用してサーバーの現(xiàn)在の時(shí)刻と日付を取得します。 get メソッドを使用してもキャッシュの問(wèn)題はありません。
HTML フォームのようにデータを POST する必要がある場(chǎng)合は、setRequestHeader() を使用して HTTP ヘッダーを追加し、send() メソッドで送信するデータを指定できます:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("POST", "demo/ajax/net/postParam.aspx", true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("webName=php中文網(wǎng)&age=3"); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>