Skip to content

Instantly share code, notes, and snippets.

@externvoid
Last active January 2, 2018 02:37
Show Gist options
  • Select an option

  • Save externvoid/edcba0d9dfd57bde5f7da41bd73efe87 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/edcba0d9dfd57bde5f7da41bd73efe87 to your computer and use it in GitHub Desktop.
同期通信テスト
<!DOCTYPE html>
<!-- https://www.ajaxtower.jp/ini/http/index3.html -->
<!-- 同期通信によるファイルの取得 -->
<html lang="ja">
<head>
<meta http-equiv="Content-Type" Content="text/html;charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>同期通信テスト</title>
<script type="text/javascript">
function loadText(){
var xmlHttp;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://stocks.hopto.org/json", false);
xmlHttp.send(null);
var data = xmlHttp.responseText;
var obj = (new Function("return " + data))();
var ar = obj["foo"];
alert(ar[0] + ar[1]);
}
</script>
</head>
<body>
<h1>同期通信テスト</h1>
<form>
<input type="button" value="ファイル読み込み" onClick="loadText()">
</form>
</body>
</html>
<!-- https://www.ajaxtower.jp/ini/http/index3.html -->
<!-- 同期通信テスト-->
@externvoid
Copy link
Author

openメソッドの第三引数はasyncならtrue, syncならfalse。syncの場合、send(null);メソッドで通信開始。本当なら、エラー処理が必要。
Functionコンストラクタ関数で文字列リテラルから、関数オブジェクトを生成し、受信したテキストをjsonに変換。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment