Last active
November 21, 2020 18:32
-
-
Save katsube/8ae87604fe334f5e4ed5c3be8773a0e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 日本工学院用の便利JSライブラリ | |
* | |
* @license MIT License | |
* @version 1.0.0 | |
* @author M.Katsube <katsubemakito@gmail.com> | |
*/ | |
const neec = { | |
//------------------------------------------- | |
// ネットワーク関係 | |
//------------------------------------------- | |
net: { | |
/** | |
* [GET] サーバからJSONを取得する | |
* | |
* @param {string} url リクエスト先のURL | |
* @param {function} callback コールバック関数 | |
* @example | |
* url = "http://g099a9999.neecbox.net/api/ping.php"; | |
* neec.net.get(url, function(status, json){ | |
* if( status && json["status"] === "OK" ){ | |
* alert("通信に成功しました"); | |
* } | |
* }); | |
*/ | |
get: function(url, callback){ | |
this.request(url, {}, callback); | |
}, | |
/** | |
* [POST]サーバからJSONを取得する | |
* | |
* @param {string} url リクエスト先のURL | |
* @param {object} param フォームデータ | |
* @param {function} callback コールバック関数 | |
* @example | |
* url = "http://g099a9999.neecbox.net/api/recive.php"; | |
* data = { | |
* id: 123, | |
* name: "foo" | |
* }; | |
* | |
* neec.net.post(url, data, function(status, json){ | |
* if( status && json["status"] === "OK" ){ | |
* alert("通信に成功しました"); | |
* } | |
* }); | |
*/ | |
post: function(url, data={}, callback){ | |
const param = { | |
method: "POST", | |
body: this.createForm(data) | |
}; | |
this.request(url, param, callback); | |
}, | |
/** | |
* リクエストを実行 | |
* | |
* @param {string} url リクエスト先のURL | |
* @param {object} param フォームデータ | |
* @param {function} callback コールバック関数 | |
*/ | |
request: function(url, param, callback){ | |
fetch(url, param) | |
.then((res)=>{ | |
if( ! res.ok ) { | |
console.error(res); | |
throw new Error(`Fetch: ${res.status} ${res.statusText}`); | |
} | |
return( res.json() ); | |
}) | |
.then((json)=>{ | |
callback(true, json); | |
}) | |
.catch((error)=>{ | |
callback(false, error); | |
}) | |
}, | |
/** | |
* フォームデータを作成する | |
* | |
* @param {object} data | |
* @return {object} FormData | |
*/ | |
createForm: function(data){ | |
const form = new FormData(); | |
for( const key in data ){ | |
form.append(key, data[key]); | |
} | |
return(form); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>通信テスト</title> | |
</head> | |
<body> | |
<script src="neec.js"></script> | |
<script> | |
neec.net.get("http://g099a9999.neecbox.net/api/ping.php", function(status, json){ | |
if(status && json["status"] === "OK"){ | |
alert("OK"); | |
} | |
else{ | |
alert("Oops..."); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment