是否可以使用jQuery从SQL Server检索数据? [英] Is it possible to retrieve data from SQL Server using jQuery?

查看:59
本文介绍了是否可以使用jQuery从SQL Server检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用jQuery从SQL Server检索数据并使用HTML控件显示数据?

Is it possible to retrieve data from SQL Server using jQuery and display the data using an HTML control ?

推荐答案

jQuery旨在在浏览器环境中运行.因此,这是一个具有DOM的环境,(显然)具有JavaScript支持.

jQuery is designed to run within a browser environment — so that's an environment with a DOM, with JavaScript support (obviously), and such.

要从MS SQL Server检索数据,您需要访问数据库驱动程序并访问服务器.浏览器很少有这两个东西,即使它们同时存在,您通常也不想让这两个东西直接交谈.

To retrieve data from MS SQL Server, you need access to a database driver, and access to the server. It's rare for the browser to have those two things, and even if they do, you usually don't want to have the two talking directly.

相反,通常的结构方式是使基于浏览器的代码与中间层服务器(Web服务器,因为浏览器擅长与之对话)进行对话,然后该中间层服务器可以访问数据库.造成这种情况的原因有很多:

Instead, the usual way you structure this is to have your browser-based code talk to a middle layer server (a web server, since browsers are good at talking to them), which then has access to the database. There are several reasons for this:

  1. 它为您提供了一个位置(浏览器和数据库之间的服务器)来应用安全性,关守,限制,监视等.

  1. It gives you a place (the server between the browser and the database) to apply security, gate-keeping, throttling, monitoring, etc.

它可以防止将数据库代码和结构暴露给最终用户(因为任何想要阅读的人都可以读取基于浏览器的JavaScript代码).

It prevents exposing your database code and structure to the end user (as your browser-based JavaScript code can be read by anyone who wants to read it).

从浏览器环境访问数据库驱动程序很棘手,需要IE的ActiveXObject之类的非标准内容,并非在所有浏览器中都存在,并且即使存在的浏览器也会触发安全警告.

Getting access to the database driver from the browser environment is tricky, requiring non-standard things like IE's ActiveXObject which aren't present on all browsers and trigger security warnings even on browsers they're present in.

如何浏览器与服务器进行对话的方式取决于您要执行的操作,但是现代实践是使用ajax,它代表了异步JavaScript和XML(在某种程度上是荒谬的). (如今,人们将其用于XML的用途远远超过XML; JSON 是一种更为常见的数据符号.)

How you have the browser talk to the server varies depending on what you want to do, but the modern practice is to use ajax, which stands (somewhat apocryphally) for Asynchronous JavaScript and XML. (Nowadays people use it for a lot more than XML; JSON is a more common data notation.)

例如,也许您想在单击按钮时填写一些HTML.在基于浏览器的代码中,单击按钮即可:

So for instance, maybe you want to fill in some HTML on a button click. In your browser-based code, you'd hook the button click:

$("#theButton").click(handleButtonClick);

您需要使用该按钮将请求发送到服务器.如果请求是幂等的(您总是返回相同的数据),则发送GET;否则,发送GET.否则,您发送POST:

You'd have that button send a request to the server. If the request is idempotent (you always get back the same data), you send a GET; otherwise, you send a POST:

function handleButtonClick() {
    $.ajax({
        url:     "/path/to/server/resource",
        type:    "GET",
        data:    {articleId: 27},
        success: function(data) {
            /* ...use the data to fill in some HTML elements... */
        },
        error:   function() {
            /* ...show an error... */
        }
    });
}

在服务器上,/path/to/server/resource上的页面将执行必要的工作,以验证是否应满足请求,连接到数据库,查询(或更新)信息并格式化响应以发送回客户端

On the server, the page at /path/to/server/resource would do the work necessary to validate that the request should be fulfilled, connect to the database, query (or update) the information, and format a response to send back to the client.

很显然,以上是关于如何执行此操作的非常非常简洁的说明,但希望它为您做好了准备,并为您提供了下一步研究的思路.

Obviously the above is a very, very, very condensed account of how you do this, but hopefully it sets the stage and gives you an idea what to look into to go to the next step.

这篇关于是否可以使用jQuery从SQL Server检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆