「マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
WCF や ASP.NET Web API で、指定の JSON を受ける(受信する)方法を説明する。
WebAPIと同様、WCFでもイイ感じにBindingしてくれる。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class JSONService : IJSONService
{
public StringResponse PostAnswersKeyValue(
KeyValRequest[] akv,
string userName, string enterpriseID, string storeID, string deviceID,
string screenID, string initializeScreenInfoID, string time)
{
・・・
// 基本的に正常系の戻り値を返す。
return new StringResponse()
{
IsError = isError,
Message = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff")
};
}
}
StringResponse?でJSONも返せる。
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(jsonArray).toString());
}
CallService("POST", url, JSON.stringify(jsonArray), "application/json; charset=utf-8", "JSON", false);
}
// ---------------------------------------------------------------
// 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, DataType, ProcessData) {
$.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.stringify(data).toString());
}
ClearWebStorage(); // 送信成功のため、WebStorageをクリア
}
// ---------------------------------------------------------------
// $.ajaxのコールバック(error)
// ---------------------------------------------------------------
// 引数 data
// 戻り値 -
// ---------------------------------------------------------------
function ServiceFailed(data) {
// Error
if (document.getElementById("response") != null) {
// <p id="response">response</p> に結果を表示
$("#response").text("response-error:" + JSON.stringify(data).toString());
}
// 送信失敗のため、WebStorageをクリアしない。
}
Tags: :.NET開発, :通信技術, :.NET Standard, :.NET Core, :ASP.NET, :ASP.NET Web API