Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a stored procedure via javascript on the client side?

Is it possible to call a stored procedure within javascript on the client side?

I know how to do on the server side, but I am interesting in doing on the client side.

Basically it boils down to directly contacting a SQL server from within the client. Is that possible?

like image 341
askolotl Avatar asked Sep 19 '25 09:09

askolotl


2 Answers

tldr; No, it is not possible to connect to SQL Server 'directly' from browser-JavaScript1.

JavaScript can "speak" HTTP and WebSockets, but SQL Server "speaks" TDS. To communicate there needs to be a common medium/protocol that both the client and server use.

  • While there are WebSocket proxies that technically make this possible it still requires a separate proxy service (and you'd still have to write/find a JavaScript TDS driver). I don't recommend eliminating the controlled access layer.

  • Likewise, an HTTP proxy where raw SQL commands are sent to/from the client could be used. I wouldn't advise this either, but some do exist.

  • External code/libraries (eg. ActiveX, Java) could establish the SQL connection and proxy through to the JavaScript client.

In all of these cases there is an intermediate helper and browser-JavaScript never connects 'directly'.


1 JavaScript is a language and this answer focuses on a browser implementation with browser-supported libraries/functions. One could argue that using node modules would still 'be JavaScript', and they would be correct .. in a different environment.

like image 158
user2864740 Avatar answered Sep 21 '25 01:09

user2864740


You cannot establish a direct connection to a database from a client's web browser. What you will need to do is create a server side application to expose an API for getting the data over HTTP.

Take a look at Microsoft's ASP.NET Web API

like image 25
Jesse Lee Avatar answered Sep 21 '25 01:09

Jesse Lee