[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>
我想将数据表内容加载到数组并使用页面方法将数组返回到前端。我尝试了一些东西但它不起作用。我的代码出了什么问题?
建议我一些想法
答案 0 :(得分:0)
嗨尝试用这样的Jquery ajax函数调用你的web方法:
<html>
<head/>
<script>
$.ajax({
type: "POST",
url: "/yourpagename.aspx/loaddata",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var dataArray = $.map(data, function (item) {
return item;
});
alert(dataArray);
console.log(dataArray);
},
error: function (data) {
alert("ajax error " + data);
}
});
script>
<body/>
<html>