JSONを受信するJSON-RPCサービスを作成する方法
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicros...
-[[戻る>JSON]]
* 目次 [#c8385b30]
#contents
*概要 [#q09445f8]
[[WCF]] や [[ASP.NET Web API]] で、指定の JSON を受ける(...
*JSONを受信するサービスを作成する [#fb585173]
**[[WCF]] の場合 [#e09891e6]
***以下のように[[WCF]]を定義する。 [#w7b3d356]
[[WebAPI>#s4811ff5]]と同様、[[WCF]]でもイイ感じにBinding...
-KeyValRequestがPOSTされたJSONで、
-ソレ以外のパラメタはQueryString
[AspNetCompatibilityRequirements(RequirementsMode = Asp...
public class JSONService : IJSONService
{
public StringResponse PostAnswersKeyValue(
KeyValRequest[] akv,
string userName, string enterpriseID, string storeID...
string screenID, string initializeScreenInfoID, stri...
{
・・・
// 基本的に正常系の戻り値を返す。
return new StringResponse()
{
IsError = isError,
Message = DateTime.Now.ToString("yyyy/MM/dd HH:mm:...
};
}
}
StringResponseでJSONも返せる。
***呼び出し側の実装(JQuery) [#mc8d6de5]
function PostJsonFromWebStorage(url) {
// Web Storageのすべての情報の取得
var jsonArray = new Array();
for (var i = 0; i < storage.length; i++) {
var _key = storage.key(i);
// Web Storageのキーと値を表示
var jsonBean = {
key: _key,
value: storage.getItem(_key)
};
jsonArray.push(jsonBean);
}
// <p id="url"></p> に表示
if (document.getElementById("url") != null) {
$("#url").text(url);
}
// <p id="request"></p> に表示
if (document.getElementById("request") != null) {
$("#request").text("request:" + JSON.stringify(j...
}
CallService("POST", url, JSON.stringify(jsonArray), ...
}
// -----------------------------------------------------...
// ajax
// -----------------------------------------------------...
// 引数
// Type : GET or POST or PUT or DELETE verb
// Url : Location of the service
// Data : Data sent to server
// ContentType : Content type sent to server
// DataType : Expected data format from server
// ProcessData : True or False
// 戻り値 -
// -----------------------------------------------------...
function CallService(Type, Url, Data, ContentType, DataT...
$.ajax({
type: Type,
url: Url,
data: Data,
cache: false,
contentType: ContentType,
dataType: DataType,
processdata: ProcessData,
success: function (data) {
// On Successfull service call
ServiceSucceeded(data);
},
error: function (data) {
// When Service call fails
ServiceFailed(data);
}
});
}
// -----------------------------------------------------...
// $.ajaxのコールバック(success)
// -----------------------------------------------------...
// 引数 data
// 戻り値 -
// -----------------------------------------------------...
function ServiceSucceeded(data) {
// Success
if (document.getElementById("response") != null) {
// <p id="response">response</p> に結果を表示
$("#response").text("response-success:" + JSON.s...
}
ClearWebStorage(); // 送信成功のため、WebStorageをク...
}
// -----------------------------------------------------...
// $.ajaxのコールバック(error)
// -----------------------------------------------------...
// 引数 data
// 戻り値 -
// -----------------------------------------------------...
function ServiceFailed(data) {
// Error
if (document.getElementById("response") != null) {
// <p id="response">response</p> に結果を表示
$("#response").text("response-error:" + JSON.str...
}
// 送信失敗のため、WebStorageをクリアしない。
}
**[[ASP.NET Web API]] の場合 [#s4811ff5]
----
Tags: [[:.NET開発]], [[:通信技術]], [[:.NET Core]], [[:AS...
終了行:
「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicros...
-[[戻る>JSON]]
* 目次 [#c8385b30]
#contents
*概要 [#q09445f8]
[[WCF]] や [[ASP.NET Web API]] で、指定の JSON を受ける(...
*JSONを受信するサービスを作成する [#fb585173]
**[[WCF]] の場合 [#e09891e6]
***以下のように[[WCF]]を定義する。 [#w7b3d356]
[[WebAPI>#s4811ff5]]と同様、[[WCF]]でもイイ感じにBinding...
-KeyValRequestがPOSTされたJSONで、
-ソレ以外のパラメタはQueryString
[AspNetCompatibilityRequirements(RequirementsMode = Asp...
public class JSONService : IJSONService
{
public StringResponse PostAnswersKeyValue(
KeyValRequest[] akv,
string userName, string enterpriseID, string storeID...
string screenID, string initializeScreenInfoID, stri...
{
・・・
// 基本的に正常系の戻り値を返す。
return new StringResponse()
{
IsError = isError,
Message = DateTime.Now.ToString("yyyy/MM/dd HH:mm:...
};
}
}
StringResponseでJSONも返せる。
***呼び出し側の実装(JQuery) [#mc8d6de5]
function PostJsonFromWebStorage(url) {
// Web Storageのすべての情報の取得
var jsonArray = new Array();
for (var i = 0; i < storage.length; i++) {
var _key = storage.key(i);
// Web Storageのキーと値を表示
var jsonBean = {
key: _key,
value: storage.getItem(_key)
};
jsonArray.push(jsonBean);
}
// <p id="url"></p> に表示
if (document.getElementById("url") != null) {
$("#url").text(url);
}
// <p id="request"></p> に表示
if (document.getElementById("request") != null) {
$("#request").text("request:" + JSON.stringify(j...
}
CallService("POST", url, JSON.stringify(jsonArray), ...
}
// -----------------------------------------------------...
// ajax
// -----------------------------------------------------...
// 引数
// Type : GET or POST or PUT or DELETE verb
// Url : Location of the service
// Data : Data sent to server
// ContentType : Content type sent to server
// DataType : Expected data format from server
// ProcessData : True or False
// 戻り値 -
// -----------------------------------------------------...
function CallService(Type, Url, Data, ContentType, DataT...
$.ajax({
type: Type,
url: Url,
data: Data,
cache: false,
contentType: ContentType,
dataType: DataType,
processdata: ProcessData,
success: function (data) {
// On Successfull service call
ServiceSucceeded(data);
},
error: function (data) {
// When Service call fails
ServiceFailed(data);
}
});
}
// -----------------------------------------------------...
// $.ajaxのコールバック(success)
// -----------------------------------------------------...
// 引数 data
// 戻り値 -
// -----------------------------------------------------...
function ServiceSucceeded(data) {
// Success
if (document.getElementById("response") != null) {
// <p id="response">response</p> に結果を表示
$("#response").text("response-success:" + JSON.s...
}
ClearWebStorage(); // 送信成功のため、WebStorageをク...
}
// -----------------------------------------------------...
// $.ajaxのコールバック(error)
// -----------------------------------------------------...
// 引数 data
// 戻り値 -
// -----------------------------------------------------...
function ServiceFailed(data) {
// Error
if (document.getElementById("response") != null) {
// <p id="response">response</p> に結果を表示
$("#response").text("response-error:" + JSON.str...
}
// 送信失敗のため、WebStorageをクリアしない。
}
**[[ASP.NET Web API]] の場合 [#s4811ff5]
----
Tags: [[:.NET開発]], [[:通信技術]], [[:.NET Core]], [[:AS...
ページ名: