Javascript 如何在javascript函数中运行sql命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7373216/
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-08-24 02:00:58  来源:igfitidea点击:

How to run sql command inside javascript function?

javascriptsql

提问by simon

Possible Duplicate:
How to connect to SQL server database from javascript?

可能的重复:
如何从 javascript 连接到 SQL 服务器数据库?

How can I run sql query inside javascript function? Hope you can help. many thanks.

如何在 javascript 函数中运行 sql 查询?希望你能帮忙。非常感谢。

回答by Arnaud Le Blanc

Unless you are using something like node.js, the javascript code is executed on the client side(in the user's browser), so it doesn't have access to the SQL server.

除非您使用类似 node.js 的东西,否则 javascript 代码是在客户端(在用户的浏览器中)执行的,因此它无法访问 SQL 服务器。

You should do the SQL query on the server side (e.g. in PHP or the language you use on the server side), and do Ajax requests on the client side.

您应该在服务器端执行 SQL 查询(例如使用 PHP 或您在服务器端使用的语言),并在客户端执行 Ajax 请求。

回答by bfavaretto

Short answer is, you can't. But you can use ajax to fire a request to the server, which runs the query and returns the results.

简短的回答是,你不能。但是您可以使用 ajax 向服务器发出请求,服务器运行查询并返回结果。

回答by szamil

JavaScript is client side, SQL is server side. Probably you just have to write a server script, like in PHP, that return sql results and with javascript and AJAX technology get that results and work on it with javascript on client side. Learn about AJAX and ie. jQuery.

JavaScript 是客户端,SQL 是服务器端。可能你只需要编写一个服务器脚本,就像在 PHP 中一样,它返回 sql 结果,并使用 javascript 和 AJAX 技术获取该结果并在客户端使用 javascript 处理它。了解 AJAX 和 ie。jQuery。