我正在從javascript調用Web方法。 web方法從northwind數據庫返回一個客戶數組。我從工作的例子是在這裏:Calling Web Services with ASP.NET AJAX使用Javascript在對象數組中創建HTML表格
我不知道如何寫這個JavaScript方法:CreateCustomersTable
這將創建HTML表來顯示返回的數據。任何幫助,將不勝感激。
我的JavaScript
function GetCustomerByCountry() {
var country = $get("txtCountry").value;
AjaxWebService.GetCustomersByCountry(country, OnWSRequestComplete, OnWSRequestFailed);
}
function OnWSRequestComplete(results) {
if (results != null) {
CreateCustomersTable(results);
//GetMap(results);
}
}
function CreateCustomersTable(result) {
alert(result);
if (document.all) //Filter for IE DOM since other browsers are limited
{
// How do I do this?
}
}
else {
$get("divOutput").innerHTML = "RSS only available in IE5+"; }
}
我的Web方法
[WebMethod]
public Customer[] GetCustomersByCountry(string country)
{
NorthwindDALTableAdapters.CustomersTableAdapter adap =
new NorthwindDALTableAdapters.CustomersTableAdapter();
NorthwindDAL.CustomersDataTable dt = adap.GetCustomersByCountry(country);
if (dt.Rows.Count <= 0)
{
return null;
}
Customer[] customers = new Customer[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
NorthwindDAL.CustomersRow row = (NorthwindDAL.CustomersRow)dt.Rows[i];
customers[i] = new Customer();
customers[i].CustomerId = row.CustomerID;
customers[i].Name = row.ContactName;
}
return customers;
}
如果您提供「results」的確切數據格式,這將會容易得多 – Triptych 2009-01-28 21:56:07
我認爲Web Method將成爲結果的來源,Customer對象數組。這是什麼意思的確切數據格式? – Picflight 2009-01-28 23:29:51