如何在 JavaScript 中连接到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16481810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:52:50  来源:igfitidea点击:

How to connect to database in JavaScript

javascriptsql

提问by Patrik18

How can I connect to mysql DB from JS function?

如何从 JS 函数连接到 mysql DB?

I found this question:

我发现了这个问题:

How to connect to SQL server database from javascript?

如何从 javascript 连接到 SQL Server 数据库?

but it doesn't work on Chrome, because they're using ActiveXObject.

但它在 Chrome 上不起作用,因为他们使用ActiveXObject.

Thank you.

谢谢你。

回答by Kasyx

There is no good solution on web browser side which will allow you to work with all browsers. There are manydisadvantages to work with database on browser site.

Web 浏览器方面没有好的解决方案可以让您使用所有浏览器。在浏览器站点上使用数据库有很多缺点。

First of all, you show your database structure and it is very dangerous. Imagine, how easier is to make SQL-injection while you know tables and fields?

首先,你展示你的数据库结构,这是非常危险的。想象一下,当您知道表和字段时,进行 SQL 注入有多容易?

You have to establish connection in some way using password which will be shown to third party users. Or you have to set password-less connection, which is also dangerous.

您必须使用密码以某种方式建立连接,该密码将显示给第三方用户。或者你必须设置无密码连接,这也很危险。

When you establish connection with database, somebody can easly execute own query what is trivial, because you are showing your structure.

当您与数据库建立连接时,有人可以轻松地执行自己的查询,因为您正在展示您的结构。

I strongly recommended you to not do it on browser side.

我强烈建议你不要在浏览器端这样做。

回答by Marcello Romani

but I have cross-domain problem, so I just need to do simple select from db

但我有跨域问题,所以我只需要从数据库中进行简单的选择

Direct browser-to-database communication is definitely nota proper solution.

浏览器到数据库的直接通信绝对不是一个合适的解决方案。

If you want to get a list of values out of the db, just write a method in whatever server-side language you prefer (or need) to use, make the client-side JavaScript code call that method through its public URI and parse the response body into some data structure.

如果您想从数据库中获取值列表,只需使用您喜欢(或需要)使用的任何服务器端语言编写一个方法,让客户端 JavaScript 代码通过其公共 URI 调用该方法并解析响应体转换成某种数据结构。

A more general solution to this kind of problems is XMLRPC (for the record, I've used it under Code Igniter / Flash ActionScript 3.0), though it's not all that simple to use.

此类问题的一个更通用的解决方案是 XMLRPC(作为记录,我在 Code Igniter / Flash ActionScript 3.0 下使用了它),尽管它使用起来并不那么简单。

If you need to get data from two different domains, then implement the above on both of them and make the JavaScript code call the two different URIs and combine the data (if needed).

如果您需要从两个不同的域中获取数据,则在这两个域上执行上述操作,并使 JavaScript 代码调用两个不同的 URI 并合并数据(如果需要)。

回答by Marcello Romani

A comment under this question

这个问题下的评论

How to make a database connection

如何建立数据库连接

suggests that's only possible with a particular mix of Microsoft technologies specifically designed to deploy desktop-like software using a web browser. The bits mentioned there are:

表明只有使用专门设计用于使用 Web 浏览器部署类似桌面的软件的特定 Microsoft 技术组合才能实现这一点。那里提到的位是:

  • HTA - HTML Applications
  • JScript - a Microsoft flavor of JavaScript
  • ActiveX objects
  • HTA - HTML 应用程序
  • JScript - 微软风格的 JavaScript
  • ActiveX 对象

A link is also provided: Introduction to HTML Applications (HTAs)

还提供了一个链接:HTML 应用程序 (HTA) 简介

回答by Pavel ?těrba

You must use AJAX, because JavaScript itself can't connect to server. You can call some PHP script with AJAX and in JavaScript handle response from it. See jQuery.ajax().

您必须使用 AJAX,因为 JavaScript 本身无法连接到服务器。您可以使用 AJAX 调用一些 PHP 脚本,并在 JavaScript 中处理来自它的响应。请参阅jQuery.ajax()

回答by praveen

make Ajax call from your javascript to php which connects you to database

从您的 javascript 到 php 进行 Ajax 调用,从而将您连接到数据库

回答by Renan

Without some sort of plugin, or sending requests to a server side application that will access the database for you, you can't. So focus on those instead, specially server side apps.

如果没有某种插件,或将请求发送到将为您访问数据库的服务器端应用程序,您就不能。所以专注于那些,特别是服务器端应用程序。