ajax:xmlhttprequest:sample
差分
このページの2つのバージョン間の差分を表示します。
ajax:xmlhttprequest:sample [2007/08/23 16:39] – 作成 - 外部編集 127.0.0.1 | ajax:xmlhttprequest:sample [2023/04/05 13:42] (現在) – 削除 nullpon | ||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== XMLHttpRequestサンプルコード ====== | ||
- | 非同期通信のサンプルコードと解説。 | ||
- | <code javascript> | ||
- | function testXMLHttpRequest(){ | ||
- | /* XMLHttpRequestオブジェクト作成 */ | ||
- | var xmlhttp = createXmlHttp(); | ||
- | if (xmlhttp == null) { | ||
- | window.alert(" | ||
- | } | ||
- | | ||
- | /* レスポンスデータ処理用のコールバック関数を設定 */ | ||
- | xmlhttp.onreadystatechange = handleHttpEvent; | ||
- | | ||
- | /* レスポンスデータ処理用のコールバック関数を定義 */ | ||
- | function handleHttpEvent(){ | ||
- | if (xmlhttp.readyState == 4) { | ||
- | if (xmlhttp.status == 200) { | ||
- | window.alert(xmlhttp.responseText); | ||
- | } else { | ||
- | window.alert(" | ||
- | } | ||
- | } | ||
- | } | ||
- | | ||
- | /* HTTPリクエスト実行 */ | ||
- | xmlhttp.open(" | ||
- | xmlhttp.send(null); | ||
- | } | ||
- | |||
- | function createXmlHttp(){ | ||
- | if (window.XMLHttpRequest) { // Mozilla, Firefox, Safari, IE7 | ||
- | return new XMLHttpRequest(); | ||
- | } else if (window.ActiveXObject) { // IE5, IE6 | ||
- | try { | ||
- | return new ActiveXObject(" | ||
- | } catch(e) { | ||
- | return new ActiveXObject(" | ||
- | } | ||
- | } else { | ||
- | return null; | ||
- | } | ||
- | } | ||
- | </ | ||
- | [[http:// | ||
- | POSTメソッドを実行する場合は、「HTTPリクエスト実行」の部分を以下のように修正 | ||
- | < | ||
- | xmlhttp.open(" | ||
- | xmlhttp.setRequestHeader(" | ||
- | xmlhttp.send(postdata); | ||
- | </ | ||
- | |||
- | postdataは、以下のようなクエリ文字列を使う。値はURLエンコーディングされている必要がある。 | ||
- | key1=value1& | ||
- | |||
- | XMLHttpRequestは、その名の通り本来はXMLデータを送信するオブジェクトで、XMLデータを直接ポストできる。 | ||
- | <code javascript> | ||
- | var xml; | ||
- | if (window.ActiveXObject) { | ||
- | xml = new ActiveXObject(" | ||
- | } else if (document.implementation) { | ||
- | xml = document.implementation.createDocument("" | ||
- | } else { | ||
- | return; | ||
- | } | ||
- | xml.appendChild(xml.createElement(" | ||
- | xmlhttp.open(" | ||
- | xmlhttp.send(xml); | ||
- | </ |
ajax/xmlhttprequest/sample.1187887146.txt.gz · 最終更新: by 127.0.0.1