希望我的问题的标题具有描述性,并有助于您理解我所面临的问题。我是编程新手,我知道我所面临的问题只是初学者会遇到的问题。请帮帮我。请耐心等待,因为这是一个很长的描述。我知道这个社区中的大多数人都是非常有经验的程序员,不需要这种详细的方法,但是我并不是想浪费时间,我相信通过提供这样的详细说明,您将能够更好地帮助我。现在关于这个问题,我正在尝试使用jQuery构建网格:
我已使用上面链接中的源代码来构建网格,但是当我运行该程序时,数据不会显示。我确定问题出在jQuery,因为我已经单独运行了Web服务,并且它连接到SQL Server并以JSON数组的形式显示输出。
我已将解决方案分为Visual Studio 2019上的三个项目:
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string GetRecipe()
{
JavaScriptSerializer js = new JavaScriptSerializer();
string recipeList = String.Empty;
List<FormGeneratorClass.FormGeneratorVar.RecipeVar> recipeCatcher = new List<FormGeneratorClass.FormGeneratorVar.RecipeVar>();
recipeCatcher = FormGeneratorClass.FormGeneratorVar.ExecuteRecipeList();
if (recipeCatcher != null && recipeCatcher.Count > 0)
{
recipeList = js.Serialize(recipeCatcher);
}
else
recipeList = js.Serialize("No recipes!");
return recipeList;
}
[WebMethod]
public string GetEmp()
{
JavaScriptSerializer js = new JavaScriptSerializer();
string EmployeeList = String.Empty;
List<FormGeneratorClass.FormGeneratorVar.EmpVar> employeeCatcher = new List<FormGeneratorClass.FormGeneratorVar.EmpVar>();
employeeCatcher = FormGeneratorClass.FormGeneratorVar.ExecuteEmployeeList();
if (employeeCatcher != null && employeeCatcher.Count > 0)
{
EmployeeList = js.Serialize(employeeCatcher);
}
else
EmployeeList = js.Serialize("No recipes!");
return EmployeeList;
}
}
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FormGeneratorClass
{
public class FormGeneratorVar
{
public class RecipeVar
{
public int Recipe_Id { get; set; }
public string Recipe_Name { get; set; }
}
public class EmpVar
{
public int Emp_Id { get; set; }
public string Emp_FirstName { get; set; }
public string Emp_LastName { get; set; }
}
public static List<RecipeVar> ExecuteRecipeList()
{
List<RecipeVar> listRecipe = new List<RecipeVar>();
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
string sqlSelectAllQuery = "SELECT * FROM Tbl_Recipe";
SqlCommand cmd = new SqlCommand(sqlSelectAllQuery, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
RecipeVar recipe = new RecipeVar();
recipe.Recipe_Id = !(rdr["recipe_id"] == DBNull.Value) ? Convert.ToInt32(rdr["recipe_id"]) : 0;
recipe.Recipe_Name = !(rdr["recipe_name"] == DBNull.Value) ? Convert.ToString(rdr["recipe_name"]) : string.Empty;
listRecipe.Add(recipe);
}
con.Close();
}
return listRecipe;
}
public static List<EmpVar> ExecuteEmployeeList()
{
List<EmpVar> listEmployee = new List<EmpVar>();
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
string sqlSelectAllQuery = "SELECT * FROM Tbl_Emp";
SqlCommand cmd = new SqlCommand(sqlSelectAllQuery, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
EmpVar employee = new EmpVar();
employee.Emp_Id = !(rdr["emp_id"] == DBNull.Value) ? Convert.ToInt32(rdr["emp_id"]) : 0;
employee.Emp_FirstName = !(rdr["emp_firstName"] == DBNull.Value) ? Convert.ToString(rdr["emp_firstName"]) : string.Empty;
employee.Emp_LastName = !(rdr["emp_lastName"] == DBNull.Value) ? Convert.ToString(rdr["emp_lastName"]) : string.Empty;
listEmployee.Add(employee);
}
con.Close();
}
return listEmployee;
}
}
}
我将把WebServicesFunctionality项目(pt.2)设置为启动项目,并对获得的结果进行截图以供参考
现在,我确信所有阅读这篇文章的人都会对我要尝试做的事情有一个更清晰的认识。因此,现在我将附上员工.aspx页面的代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmployeeRecord.aspx.cs" Inherits="PracticeValidation.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Record of employees</title>
<meta name="description" content="JavaScript Grid with rich support for Data Filtering, Paging, Editing, Sorting and Grouping" />
<link href="Scripts/jqx.base.css" rel="stylesheet" type="text/css"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1 maximum-scale=1 minimum-scale=1" />
<script src="Scripts/jquery-1.11.1.min.js"></script>
<script src="Scripts/jqxcore.js"></script>
<script src="Scripts/jqxdata.js"></script>
<script src="Scripts/jqxbuttons.js"></script>
<script src="Scripts/jqxscrollbar.js"></script>
<script src="Scripts/Menu.js"></script>
<script src="Scripts/jqxcheckbox.js"></script>
<script src="Scripts/jqxlistbox.js"></script>
<script src="Scripts/jqxdropdownlist.js"></script>
<script src="Scripts/jqxgrid.js"></script>
<script src="Scripts/jqxgrid.sort.js"></script>
<script src="Scripts/jqxgrid.pager.js"></script>
<script src="Scripts/jqxgrid.selection.js"></script>
<script src="Scripts/jqxgrid.edit.js"></script>
<script src="Scripts/demos.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//Getting the source data with ajax GET request
source = {
datatype: "json",
datafields: [
{ name: 'EmpID' },
{ name: 'EmpLastName' },
{ name: 'EmpFirstName' }
],
async: false,
record: 'Table',
url: 'WebService1.asmx/GetEmp',
};
var dataAdapter = new $.jqx.dataAdapter(source,
{ contentType: 'application/json; charset=utf-8' }
);
$("#grid").jqxGrid({
source: dataAdapter,
theme: 'classic',
width: '100%',
columns: [
{ text: 'EmpID', dataField: 'EmpID', width: 250, hidden: false },
{ text: 'EmpLastName', dataField: 'EmpLastName', width: 150 },
{ text: 'EmpFirstName', dataField: 'EmpFirstName', width: 180 },
]
});
});
</script>
</head>
<body class ='default'>
<form id="form1" runat="server">
<div>
<h1>Welcome to the record of employees page</h1>
<h4>Click <a href="HomePage.aspx">here</a> to go back to the main login page</h4>
</div>
<div id="grid">
</div>
</form>
</body>
</html>
最后,我将附上我得到的输出的屏幕截图。
感谢所有阅读整篇文章并保持安全的人!
我弄清楚哪里出了问题。有很多原因导致数据没有按照我想要的方式绑定到网格。我希望这对其他遇到相同问题的人有所帮助。
如果要像我一样创建解决方案,则需要解决的第一个问题是需要创建一个代理Web服务,该代理Web服务从Web服务获取数据。这是因为Web服务项目和Web表单项目在不同的端口上运行。因此,即使一切正常,发出AJAX请求时也会收到404错误。因此,可以从Web表单项目中创建Web服务代理,或者放弃创建单独的Web服务项目的想法。
我遇到的下一个问题是笨拙,花了我很长时间才弄清楚。无论将来自SQL的数据序列化为JSON,实际上从Web服务发送回的数据都是XML。这是因为Web服务使用SOAP,因此默认情况下,数据传输的方法是XML。如果您运行Web服务并观察数据,则将看到带有XML包装器的JSON字符串。如果您打开Chrome开发工具并查看内容类型,您会看到其返回XML,并且无论您做什么,都无法阻止Web服务通过AJAX“ GET”请求返回XML。那么,您如何解决问题呢?两种方式:
方法1:使用Web API代替Web服务。这将允许您使用REST服务。
方法2:如果您坚持使用Web服务,那么以下链接将非常有帮助。https://www.one-tab.com/page/IjhbYbayRlWka-8ruNI87w
在我得到AJAX之后,从Web服务成功返回JSON。下一个问题是将数据绑定到网格插件。这非常简单明了。找到您要使用的方法的演示并将其复制粘贴到AJAX请求的成功回调函数中。您甚至可以通过在AJAX请求的成功回调函数内部调用用户定义函数来传递接收的数据。
有时,使用AJAX请求使用Web服务时,您可能会遇到问题,该请求说“序列化或反序列化期间发生错误。超过了JSON最大长度属性”。如果您确实遇到这种情况,请尝试将javascriptSerializer对象初始化为maxInt值。
如果您使用的是Newtonsoft .JSON,请检查是否使用常规javascriptSerializer类对数据进行序列化。我之所以这样说,是因为AJAX使用javascriptSerializer和Newtonsoft .JSON请求序列化数据倾向于忽略循环引用错误。结果,当您的Web服务实际上正在运行循环引用错误时,Ajax函数可能会引发“序列化或反序列化期间发生错误。超出了JSON最大长度属性”错误。在这种情况下,请考虑对要使用的SP或查询进行更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句