javascript - 连接javascript访问数据库

标签 javascript database

你想知道我是否可以用 javascript 将我的网站连接到 acess 数据库,如果可以我该怎么做? 这是代码:

<!DOCTYPE html>
<html>

<!--Here you will create a polygon by tools that Google maps "drawingManager" gives to you and then you have an array named "coordinates" as an output. This array saves all latLng of the polygon. When you edit the polygon it also edit this variable too.
!-->

<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=drawing"></script>

<!--Global variables!-->
<script>
//This variable gets all coordinates of polygone and save them. Finally you should use this array because it contains all latitude and longitude coordinates of polygon.
var coordinates = [];

//This variable saves polygon.
var polygons = [];
</script>

<script>
//This function save latitude and longitude to the polygons[] variable after we call it.
function save_coordinates_to_array(polygon)
{
    //Save polygon to 'polygons[]' array to get its coordinate.
    polygons.push(polygon);

    //This variable gets all bounds of polygon.
    var polygonBounds = polygon.getPath();

    for(var i = 0 ; i < polygonBounds.length ; i++)
    {
        coordinates.push(polygonBounds.getAt(i).lat(), polygonBounds.getAt(i).lng());
        alert(i);
    }   
}
</script>

<script>
function initialize()
{
    //Create a Google maps.
    var map = new google.maps.Map(document.getElementById('map'),
     {zoom: 12, 
     center: new google.maps.LatLng(32.344, 51.048)});

    //Create a drawing manager panel that lets you select polygon, polyline, circle, rectangle or etc and then draw it.
    var drawingManager = new google.maps.drawing.DrawingManager();
    drawingManager.setMap(map);

    //This event fires when creation of polygon is completed by user.
    google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon) {
        //This line make it possible to edit polygon you have drawed.
        polygon.setEditable(true);

        //Call function to pass polygon as parameter to save its coordinated to an array.
        save_coordinates_to_array(polygon);

        //This event is inside 'polygoncomplete' and fires when you edit the polygon by moving one of its anchors.
        google.maps.event.addListener(polygon.getPath(), 'set_at', function () {
            alert('changed');
            save_coordinates_to_array(polygon);
            });

        //This event is inside 'polygoncomplete' too and fires when you edit the polygon by moving on one of its anchors.
        google.maps.event.addListener(polygon.getPath(), 'insert_at', function () {
            alert('also changed');
            save_coordinates_to_array(polygon);
            });
    });
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<form id="form1" runat="server">
    <asp:AccessDataSource id="AccessDataSource1" runat="server">
    </asp:AccessDataSource>
    <asp:AccessDataSource id="AccessDataSource2" runat="server">
    </asp:AccessDataSource>
    <div id="map" style="width:1024px; height:768px;">
    </div>
</form>
</body>

</html>

我想做的是将坐标保存在数据库中,并从数据库中获取坐标,当然。 这可能吗?

最佳答案

有人可能已经找到了一种方法来做到这一点,但在我看来这是一个非常糟糕的主意。您会将您的数据库连接信息暴露给任何有权访问您的 javascript 的人(假设这是一个典型的网站),这就是...每个人。这为所有类型的黑客敞开了大门。

最常见的方法是使用某种服务器端语言,例如 C#/VB.NET 或 php(尽管还有很多其他语言)。

您的 Javascript 使用 ajax 调用服务器端,您的服务器端代码连接到数据库并返回响应。

关于javascript - 连接javascript访问数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24427269/

相关文章:

javascript - 允许远程服务器接受来自本地主机的带有参数的请求

sql - 从查询结果中选择?

ios - Swift - 如何将图像保存到数组中并访问它们?

javascript - 推送到轻量级 HTML5 客户端的 ZeroMQ 套接字

javascript - 以后端期望的特定格式重构 JS 数组

php - 如何将 MySQL 数据库连接到 iPage 中的域?

Android Firebase 持久化集

javascript - 我是否需要转换 HTMLCollection 或 Nodelist 来创建节点数组?

javascript - 捕获 JavaScript 迭代器的返回值

javascript - 设置主体颜色但 div block 有不同的颜色