【发布时间】:2014-07-01 23:19:15
【问题描述】:
我在 ASP.NET 中有一个网站,我正在使用 ScriptManager 通过 AJAX 从 WebService 获取数据。
加载 default.aspx 页面时,会触发 onload 事件并从我的 Javascript 调用 getCategoryDataSet() 函数。 javascript中的getCategoryDataSet()函数只是调用webservice中的方法来获取数据。
问题:
当 getCategoryDataSet() 被调用时,这是我得到的错误消息:
“JavaScript 运行时错误:无法获取未定义或空引用的属性‘0’”
当 getCategoryDataSet() 函数被触发时,似乎函数在接收到来自 Web 服务的数据之前就结束了。我这样说是因为我在访问 web 服务的数据之前添加了一个 alert() 函数,而且似乎我在警报框上按下 ok 的时间足以从 web 服务调用中检索数据。
如何在不使用提醒按钮为通话留出更多时间的情况下解决此问题?还是其他问题?
以下照片显示了使用警报框的代码:
这是我的解决方案资源管理器:
我的 default.aspx 代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="category_selection_02._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="script/JScript_01_GetCategories.js"></script>
</head>
<body onload="getCategoryDataSet()">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService_GetCategories.asmx" />
</Services>
</asp:ScriptManager>
<div>
</div>
<div id="divListBoxes">
THIS DIV WILL BE POPULATED WITH LISTBOXES
</div>
</form>
</body>
</html>
我的 WebService_GetCategories.asmx.cs 代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService_GetCategories : System.Web.Services.WebService
{
[WebMethod]
public List<Category> GetCategoriesWebService(int index_left)
{
//Debug.WriteLine("in WEBMETHOD");
List<Category> listCategory = new List<Category>();
DataSet ds = (new DataBase_DataSet_Generator(index_left)).getDataSet();
DataTable dt = new DataTable();
dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
Category categoryFields = new Category();
categoryFields.category_id = (int)dr["category_id"];
categoryFields.index_left = (int)dr["index_left"];
categoryFields.index_right = (int)dr["index_right"];
categoryFields.categoryName = (dr["categoryName"]).ToString();
//categoryFields.categoryInfo= (dr["categoryInfo"]).ToString();
listCategory.Add(categoryFields);
//Debug.WriteLine("This is the total items in the listCategory: "+ listCategory.Count);
}
return listCategory;
}
}
我的 JScript_01_GetCategories.js 代码:
var public_categoryDataSet; //this is the category dataset retreived from the webservice
function getCategoryDataSet() {
var index_left = 1;
category_selection_02.WebService_GetCategories.GetCategoriesWebService(index_left, GetCategoryIdSuccessCallBack, GetCategoryIdFailedCallBack)
function GetCategoryIdSuccessCallBack(results_from_webservice) {
public_categoryDataSet = results_from_webservice;
}
function GetCategoryIdFailedCallBack(errors) {
alert("AJAX Failed callback invalid data inserted in textbox");
}
alert("Alert Fired"); //<--if this alert() call is removed I get an error???
alert(public_categoryDataSet[0].category_id);
}
【问题讨论】:
标签: javascript jquery asp.net ajax web-services