2024-07-08
XMLHttpRequest, jQuery.ajax, fetchいずれかの方法があるが、同期・非同期が簡単に使い分けられるjQuery.ajaxがおすすめ。
https://api.jquery.com/jquery.ajax/
以下のような、Solidity ERC-721 abi.jsonファイルをダウンロードするものとする。
| JSON | abi.json | GitHub Source |
[
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "ownerOf",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
]
同期(その場で終わるまで待ってから次を実行)で使う場合
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>//<![CDATA[
var abi_json = null;
console.log("before ajax call");
$.ajax({
type: 'GET',
url: 'abi.json',
data: 'a=b&c=d',
async : false,
dataType: 'text',
success: function(data) {
console.log("success ajax call");
if(data != null) {
console.log(data);
abi_json = JSON.parse(data);
console.log(abi_json);
}
}
});
console.log("after ajax call");
//]]></script>
typeにはメソッドを指定、urlにはこのhtmlからの相対パスか、httpsから始まる絶対パスを指定。
dataにはURLに渡すパラメータを指定。URLがCGIプログラムである場合などに指定。
asyncをfalseに設定することで、同期通信となり、ダウンロードが終わるまで次へ行かない。
dataTypeには文字列でダウンロード対象のファイルタイプを指定する。
textを指定すると、テキストデータとして持ってくるので、consoleで確認したあと、あらためてJSON.parseして文字列→JSON変換するのがおすすめ。
いきなりデータタイプにjsonを指定することもできる。その場合JSON.parseは不要で、dataにはダイレクトにJSON型のデータが入る。
実行結果console.log
before ajax call success ajax call after ajax call
before→success→afterと、通信が終わるのを待ってから順番に実行されているのが確認できる。
非同期(バックグラウンドで実行しながら)で使う場合
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>//<![CDATA[
var abi_json = null;
console.log("before ajax call");
$.ajax({
type: 'GET',
url: 'abi.json',
data: 'a=b&c=d',
async : true,
dataType: 'text',
success: function(data) {
console.log("success ajax call");
if(data != null) {
console.log(data);
abi_json = JSON.parse(data);
console.log(abi_json);
}
}
});
console.log("after ajax call");
//]]></script>
asyncをtrueに設定することで、非同期通信となり、バックグラウンドへ移行しすぐ次の処理へ行く。
終わったらsuccessがあとから呼ばれることになる。
実行結果console.log
before ajax call after ajax call success ajax call
before→after→successと、通信が終わるのを待たずにすぐ次へ行って後からsuccessが呼ばれているのが確認できる。
よって、グローバル変数abi_jsonがnullの場合まだ通信が終わっていないかエラーかということなので、そこを判定して処理を分ける。
※本記事内容の無断転載を禁じます。
ご連絡は以下アドレスまでお願いします★
LetsEncrypt/certbotの証明書自動更新がエラーになる場合
Wav2Lipのオープンソース版を改造して外部から呼べるAPI化する
Wav2Lipのオープンソース版で静止画の口元のみを動かして喋らせる
【iOS】アプリアイコン・ロゴ画像の作成・設定方法
オープンソースリップシンクエンジンSadTalkerをAPI化してアプリから呼ぶ【2】
オープンソースリップシンクエンジンSadTalkerをAPI化してアプリから呼ぶ【1】
【Xcode】iPhone is not available because it is unpairedの対処法
【Let's Encrypt】Failed authorization procedure 503の対処法
【Debian】古いバージョンでapt updateしたら404 not foundでエラーになる場合
【Windows10】リモートデスクトップ間のコピー&ペーストができなくなった場合の対処法
CUDA13環境下でGPU使用版のPyTorchを導入する
Windows11+WSL2でUbuntuを使う【2】ブリッジ接続+固定IPの設定
【Apache】サーバーに同時接続可能なクライアント数を調整する
LinuxからWindowsの共有フォルダをマウントする
【C/C++】小数点以下の切り捨て・切り上げ・四捨五入
size_tとssize_tを使い分けてSegmentation Faultを予防する
Googleスプレッドシートを編集したら自動で更新日時を入れる
Windows11のコマンドプロンプトでテキストをコピーする