IRIS/REST APIを呼び出す簡単なサンプル
これは InterSystems FAQ サイトの記事です。
まず以下のようなREST APIを定義したクラスを作成します。
Class User.REST Extends%CSP.REST
{
Parameter HandleCorsRequest = 1;
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/req1" Method="GET" Call="req1"/>
<Route Url="/req2/:value" Method="POST" Call="req2"/>
<Route Url="/req3" Method="GET" Call="req3"/>
</Routes>
}
ClassMethod req1() As%Status
{
write$G(^test)
quit$$$OK
}
ClassMethod req2(value As%String) As%Status
{
set^test=$g(value)
quit$$$OK
}
ClassMethod req3() As%Status
{
write$ZDT($H)
quit$$$OK
}
}
次にこのREST APIを呼び出すHTMLファイル(test.html)を作成します。
<htmlng-app="xxx"><head><title>REST API Test</title><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script><scriptlanguage="JavaScript">var app=angular.module('xxx',[]);
app.controller('AppController', ['$scope', '$http', function($scope, $http){
// running on load
$http.get('http://localhost:52773/csp/rest/req1').then (
function(res){ $scope.x=res.data; }
,function(res){ $scope.x="NG!!!"; }
);
// running on submit
$scope.test2 = function(){
alert($scope.name);
$http.post('http://localhost:52773/csp/rest/req2/'+$scope.name).then (
function(res){ location.reload(); }
,function(res){ alert("NG"); }
);
};
// runnning on click
$scope.test3 = function(){
$http.get('http://localhost:52773/csp/rest/req3').then (
function(res){ alert(res.data); }
,function(res){ alert("NG"); }
);
};
}
])
</script></head><bodyng-controller="AppController">
^test : {{x}}<br><br><formng-submit="test2()">
update ^test: <inputtype="text"ng-model="name"><inputtype="submit"value="Save!"></form><inputtype="button"value="OnClick"ng-click="test3()"></body></html>
------------------------------------------------------
サンプル実行方法
------------------------------------------------------
(1) test.html をデスクトップなど適当なフォルダにおき、中身の接続先のポート番号を、管理ポータルと同じものに変更して保存。
(2) REST.cls を IRIS にインポート
(3) ポータル > システム管理 > セキュリティ > アプリケーション>ウェブ・アプリケーション > 新しいウェブ・アプリケーション作成
名前: /csp/rest
ネームスペース: REST.clsのネームスペース
ディスパッチクラス: User.REST
(4) ターミナルで ^test を登録
set ^test=123
(5) test.html をダブルクリックで実行
ディスカッション (0)1