简体   繁体   English

如何从DB2获取数据到AngularJS应用程序?

[英]How to get data from DB2 into an AngularJS application?

I want to create an AngularJS application where I need to get the data from a DB2 database. 我想创建一个AngularJS应用程序,我需要从DB2数据库中获取数据。

How do I connect to DB2 directly from my AngularJS application? 如何直接从我的AngularJS应用程序连接到DB2? I couldn't find any references online. 我在网上找不到任何参考资料。

Simple 简单

  1. Create rest API endpoints to fetch data eg yourdomain/get/data in your DB2 server 创建其余API端点以获取DB2服务器中的数据,例如yourdomain/get/data
  2. From your Angular app, make Ajax call to above end points 从您的Angular应用程序中,将Ajax调用到上述端点

An AngularJS App doesn't directly connect to a DB2 database, it needs a server-side component to connect to the database. AngularJS App不直接连接到DB2数据库,它需要服务器端组件来连接数据库。

Connect with any server-side framework to get a response from the database, for example PHP or node.js. 连接任何服务器端框架以从数据库获取响应,例如PHP或node.js.

Here is an example to how to connect, I will be using PHP to get the server-side response. 这是一个如何连接的示例,我将使用PHP来获取服务器端响应。

In html: 在html中:

<div ng-app="myApp" ng-controller="myCtrl">
<form>
     <button type="button" ng-click="getData()">GetResponse</button>
</form>
</div>

In AngularJS: 在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){
        }
        });
    };
});

In PHP: 在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 

You need a server side component that talk to database and provides the data (eg json data) over http. 您需要一个与数据库通信的服务器端组件,并通过http提供数据(例如json数据)。 The component can be written in your own language choice eg php, java etc. The angular shall talk to this API using Ajax calls and get the data that way. 该组件可以用您自己的语言选择编写,例如php,java等。角度应该使用Ajax调用与此API通信并以这种方式获取数据。

AngularJS looks for some sort of REST (or REST-like) server to give it JSON results. AngularJS寻找某种REST(或类似REST)服务器来为其提供JSON结果。 In its default state DB2 requires some form of ODBC or JDBC driver to connect it into a programming language but it doesn't provide a REST-like interface. 在默认状态下,DB2需要某种形式的ODBC或JDBC驱动程序将其连接到编程语言中,但它不提供类似REST的接口。

Now that we've established this, let's look at a few ways you can solve your problem. 现在我们已经建立了这个,让我们看看你可以解决问题的几种方法。

IBM offers some free DB2 User Defined Functions that speak REST and can be added to your instance. IBM提供了一些免费的DB2用户定义函数 ,它们可以说REST,并且可以添加到您的实例中。 There is a download link contained in the linked article. 链接文章中包含下载链接。

There is also an open source service from DreamFactory that does essentially the same thing - it drops a REST API in front of DB2. 还有一个来自DreamFactory的开源服务基本上做同样的事情 - 它在DB2之前删除了一个REST API。

I'd suggest that you investigate both options and see which one makes the most sense to you based on your requirements. 我建议您调查这两个选项,并根据您的要求查看哪个选项对您最有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM