如何使用asp.net从数据库中检索数据,然后将该数据提供给javascript数组?

时间:2021-12-12 04:59:55

Basically which would be the simplest way to get data from a database and feed it into a javascript array without the user being able to see the data?

基本上哪种方法是从数据库获取数据并将其提供给javascript数组而用户无法查看数据的最简单方法?

I'm making a simple hangman game web app as a college project and I basically have to have the game's list of words in a database, which I've done. I also know how to connect and how put database data into a data source or how to read the data manually using a reader.

我正在制作一个简单的刽子手游戏网络应用程序作为大学项目,我基本上必须在数据库中拥有游戏的单词列表,我已经完成了。我也知道如何连接以及如何将数据库数据放入数据源或如何使用阅读器手动读取数据。

What I want to know is how to feed the data (not the whole database but just the data that I read in) into javascript. For example let's say I've got 1000 values in the database and I've extracted 15 different values from the database in the ASP.NET PageLoad event, now what do I do to get these 15 values into JS without enabling the user to see these vales by checking the page source?

我想知道的是如何将数据(不是整个数据库,而只是我读过的数据)提供给javascript。例如,假设我在数据库中有1000个值,并且我在ASP.NET PageLoad事件中从数据库中提取了15个不同的值,现在我该怎么做才能将这15个值放到JS中而不让用户看到这些vales通过检查页面源?

1 个解决方案

#1


2  

From your question, it sounds like you are working with ASP.NET web forms. If that is the case, you will probably want to retrieve your 15 values using an AJAX call. If you build the JS synchronously in the Page_Load, it will be easy to view source and see what is going on... That said, anybody can turn on Network capture and see the hangman values coming back in the AJAX response.

从您的问题来看,这听起来像是在使用ASP.NET Web表单。如果是这种情况,您可能希望使用AJAX调用检索15个值。如果您在Page_Load中同步构建JS,则可以轻松查看源代码并查看正在发生的事情...也就是说,任何人都可以打开网络捕获并查看AJAX响应中返回的hangman值。

Maybe consider using a page method. You would make a static web method in your code behind. Here is what pseudo code would look like with C# and jQuery.

也许考虑使用页面方法。您可以在后面的代码中创建一个静态Web方法。下面是使用C#和jQuery的伪代码。

On the server in your code behind (say it is hangman.aspx.cs)

在代码后面的服务器上(比如说是hangman.aspx.cs)

[WebMethod]
[ScriptMethod]
public static string[] GetHangmanValues()
{
    string[] values = new string[15];
    //retrieve your hangman values from the db and return them
    return values;
}

On the client side:

在客户端:

var HANGMAN_APP = {};
$(function () {
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: "hangman.aspx/GetHangmanValues",
        data: "{}",
        success: function (data) {
            //Microsoft page methods wrap the data in data.d
            //use data.d to populate HANGMAN_APP
        },
        error: function () {
            alert("Error calling the page method.");
        }
    });
});

This way of doing it will keep the values from being displayed in page source, but I just want to reiterate: Network traffic and/or a javascript debugger would not protect your values if somebody really wanted to see them. Also, an extra benefit is that Page Methods don't go through the entire page life cycle or deal with ViewState (like UpdatePanels do), which makes them very efficient for this sort of thing.

这种方式可以防止值显示在页面源中,但我只想重申:如果有人真的想看到它们,网络流量和/或javascript调试器将无法保护您的值。此外,一个额外的好处是页面方法不会经历整个页面生命周期或处理ViewState(如UpdatePanels do),这使得它们非常有效地处理这类事情。

#1


2  

From your question, it sounds like you are working with ASP.NET web forms. If that is the case, you will probably want to retrieve your 15 values using an AJAX call. If you build the JS synchronously in the Page_Load, it will be easy to view source and see what is going on... That said, anybody can turn on Network capture and see the hangman values coming back in the AJAX response.

从您的问题来看,这听起来像是在使用ASP.NET Web表单。如果是这种情况,您可能希望使用AJAX调用检索15个值。如果您在Page_Load中同步构建JS,则可以轻松查看源代码并查看正在发生的事情...也就是说,任何人都可以打开网络捕获并查看AJAX响应中返回的hangman值。

Maybe consider using a page method. You would make a static web method in your code behind. Here is what pseudo code would look like with C# and jQuery.

也许考虑使用页面方法。您可以在后面的代码中创建一个静态Web方法。下面是使用C#和jQuery的伪代码。

On the server in your code behind (say it is hangman.aspx.cs)

在代码后面的服务器上(比如说是hangman.aspx.cs)

[WebMethod]
[ScriptMethod]
public static string[] GetHangmanValues()
{
    string[] values = new string[15];
    //retrieve your hangman values from the db and return them
    return values;
}

On the client side:

在客户端:

var HANGMAN_APP = {};
$(function () {
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: "hangman.aspx/GetHangmanValues",
        data: "{}",
        success: function (data) {
            //Microsoft page methods wrap the data in data.d
            //use data.d to populate HANGMAN_APP
        },
        error: function () {
            alert("Error calling the page method.");
        }
    });
});

This way of doing it will keep the values from being displayed in page source, but I just want to reiterate: Network traffic and/or a javascript debugger would not protect your values if somebody really wanted to see them. Also, an extra benefit is that Page Methods don't go through the entire page life cycle or deal with ViewState (like UpdatePanels do), which makes them very efficient for this sort of thing.

这种方式可以防止值显示在页面源中,但我只想重申:如果有人真的想看到它们,网络流量和/或javascript调试器将无法保护您的值。此外,一个额外的好处是页面方法不会经历整个页面生命周期或处理ViewState(如UpdatePanels do),这使得它们非常有效地处理这类事情。