[英]How to get data from DB2 into an AngularJS application?
我想創建一個AngularJS應用程序,我需要從DB2數據庫中獲取數據。
如何直接從我的AngularJS應用程序連接到DB2? 我在網上找不到任何參考資料。
簡單
yourdomain/get/data
AngularJS App不直接連接到DB2數據庫,它需要服務器端組件來連接數據庫。
連接任何服務器端框架以從數據庫獲取響應,例如PHP或node.js.
這是一個如何連接的示例,我將使用PHP來獲取服務器端響應。
在html中:
<div ng-app="myApp" ng-controller="myCtrl">
<form>
<button type="button" ng-click="getData()">GetResponse</button>
</form>
</div>
在AngularJS中:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.getData = function () {
$http({
method: 'POST',
url: "Path to Backend Handling File",
data:{};
}).then(function success(response) {
console.log(response.data);
}, function error(response){
}
});
};
});
在PHP中:
// This is an example code written in PHP
// Let $data will have response from database
print_r(json_encode($data)); //used to return reponse to angular script used.
// Now response is stored in the response variable of success function as JSON which can be used as needed
您需要一個與數據庫通信的服務器端組件,並通過http提供數據(例如json數據)。 該組件可以用您自己的語言選擇編寫,例如php,java等。角度應該使用Ajax調用與此API通信並以這種方式獲取數據。
AngularJS尋找某種REST(或類似REST)服務器來為其提供JSON結果。 在默認狀態下,DB2需要某種形式的ODBC或JDBC驅動程序將其連接到編程語言中,但它不提供類似REST的接口。
現在我們已經建立了這個,讓我們看看你可以解決問題的幾種方法。
IBM提供了一些免費的DB2用戶定義函數 ,它們可以說REST,並且可以添加到您的實例中。 鏈接文章中包含下載鏈接。
還有一個來自DreamFactory的開源服務基本上做同樣的事情 - 它在DB2之前刪除了一個REST API。
我建議您調查這兩個選項,並根據您的要求查看哪個選項對您最有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.