[System.Web.Services.WebMethod]
public Array loaddata()
string sql = "SELECT Name,Time,Inuse FROM table4";
using (SqlConnection Connection = new SqlConnection((@"Data Source")))
{
using (SqlCommand myCommand = new SqlCommand(sql, Connection))
{
Connection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(myReader);
Connection.Close();
DataView dv = new DataView(dt);
dv.RowFilter = (("Name='ACVX'"));
var tableEnumerable = dv.ToTable().AsEnumerable();
var tableArray = tableEnumerable.ToArray();
return tableArray ;
}
}
}
//Front End
<html>
<head/>
<script>
PageMethods.loaddata(LoadSucc, LoadFail);
function LoadSucc(obj) //obj is array returned from back end{obj-tablearray]
{
var goog = [];
goog = Object.values(obj);
//I want load the obj into my array goog.
}
function LoadFail() {
alert("Data missing");
}
script>
<body/>
<html>
我想加載我的數據表內容到數組,並使用頁面方法將數組返回到前端。我嘗試了一些東西,但它不會工作。我的代碼有什麼問題?
建議我的一些想法從後端到前端返回數組
有你在調試控制檯鉻在功能obj的?它是空的嗎?有時它會在數據字段中包裝響應,因此您必須執行類似obj.data的操作.. –