javascript - 将 C# 数据结果传递给 JavaScript 变量

标签 javascript c# asp.net webmethod

我需要将下面的 SQL 结果传递给 ASP.Net 中的 Javascript?我试图在 JS 中声明这两个字段,但无法正确声明。如何在 JS 中获取结果?

     var Description = "<%=this.Description%>"
     var ApplicationSourceCount = "<%=this.ApplicationSourceCount%>"

在C#中声明字符串

    public class ApplicantSourceData
    {
        public string Description { get; set; }
        public string ApplicantSourceCount { get; set; }
    }

C# Web 方法

[WebMethod]
    public List<ApplicantSourceData> GetApplicantSourceData(List<string> aData)
    {
        //SqlDataReader reader;
        List<ApplicantSourceData> GetApplicantSourceData = new List<ApplicantSourceData>();

        string connectionString = ConfigurationManager.ConnectionStrings["ATL2"].ConnectionString;
        string commandTextApplicantsByMonthCount = Properties.Queries.commandTextApplicantsByMonthCount;

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(commandTextApplicantsByMonthCount))
            {

                command.CommandText = commandTextApplicantsByMonthCount;
                command.CommandType = CommandType.Text;
                command.Connection = con;
                con.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    int counter = 0;
                    while (reader.Read())
                    {
                        ApplicantSourceData tsData = new ApplicantSourceData();
                        tsData.Description = reader["Description"].ToString();
                        tsData.ApplicantSourceCount = reader["ApplicantSourceCount"].ToString();
                        GetApplicantSourceData.Add(tsData);
                        counter++;
                    }
                }
            }
            return GetApplicantSourceData;
        }
    }

我已经尝试了以下方法,但是没有

最佳答案

您应该从客户端调用 WebMethod(例如,作为 AJAX 请求)。然后,您应该让 WebMethod 向客户端返回包含请求数据的响应(例如,作为 JSON 格式的字符串)。

有几种方法可以做到这一点。第一种是使用 AJAX。

例子:

C#:

[WebMethod]
public static string someWebMethod(String data) {

    // your code here
    var returnData = /* some kind of data */
    JavaScriptSerializer json = new JavaScriptSerializer();
    return json.Serialize(returnData);

}

JS(以 jQuery 为例,但您也可以在常规 JS 中执行此操作):

$.ajax({
  type: "POST",
  url: "PageName.aspx/someWebMethod",
  data: "{"data": data}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(response) {
    // Do something with the response.
  }
});

另一种选择是使用 PageMethods。为此,您可以按照如下模式从 JS 调用该方法:

PageMethods.someWebMethod(data);

function onSucess(result) {
    console.log(result);
}

function onError(result) {
    console.log(result);
}

我建议您也多看看 ASP.NET WebMethod 文档。此外,这里有一些教程可能会有所帮助:Calling ASP.NET WebMethod using jQuery-AJAXCalling an ASP.NET C# Method (Web Method) Using JavaScript .

关于javascript - 将 C# 数据结果传递给 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50697096/

相关文章:

javascript - 如何制作多个嵌套对象的数组?

javascript - 使用 HTML DOM JS 分配事件

c# - Visual Studio 2017 MVC 不包含所有源文件夹

c# - 如何为具有多个目标的 CSPROJ 生成 XML 文档

c# - RadioButtonList 值的空引用异常

asp.net - 将 aspnetdb 数据库与另一个数据库合并?

asp.net - 如何从 jQuery 将 int 值传递给 ASP.NET 页面方法?

.net - 将现有 ASP.NET MVC 应用程序与 Orchard CMS 集成

javascript - CKeditor 不更新源代码模式下的文本区域

javascript - 增加页面加载时间