客户端代码
<script type="text/javascript">
$(function () {
$('#tabList').datagrid({
title: 'DataGrid',
width: 700,
height: 220,
fitColumns: true,
nowrap: false,
rownumbers: true,
showFooter: true,
url: "WebService/wsTest.asmx/GetGrid",
//url: "WebService/wsTest.ashx",
onLoadSuccess: function (data) {
alert(1);
},
onLoadError: function (data) {
alert(2);
},
columns: [[
{ title: '用户ID', field: 'id', width: 130, sortable: true },
{ field: 'name', title: '用户名称', width: 240, sortable: true }
]],
});
$(".datagrid-view").css({ width: "300px", height: "205px" });
});
</script>
webservice代码
public class Person
{
public string name;
public int id;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetGrid()
{
Person[] plist = new Person[2];
plist[0] = new Person();
plist[0].name = "zwm";
plist[0].id = 1;
plist[1] = new Person();
plist[1].name = "zwb";
plist[1].id = 2;
return "{\"total\":2,\"rows\":" + new JavaScriptSerializer().Serialize(plist) + "}";
}
在easyui中,gridview无法接受webservice的返回值,主要因为webservice的返回为soap的xml格式
如果换作ashx的respose则可以返回正确的结果
那么我们是否可以用webservice跟ashx一样返回呢?
我在这里新建了个ashx的文件
看到里面有这么一个代码段
public void ProcessRequest (HttpContext context)
里面有个context的参数,类型为HttpContext
这个参数里面包含了
context.Response.Write方法
哈哈~~顺着这条线,我在webservice里面也找到了这个参数
把webservice代码改写如下
public class Person
{
public string name;
public int id;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetGrid()
{
Person[] plist = new Person[2];
plist[0] = new Person();
plist[0].name = "zwm";
plist[0].id = 1;
plist[1] = new Person();
plist[1].name = "zwb";
plist[1].id = 2;
this.Context.Response.Write("{\"total\":2,\"rows\":" + new JavaScriptSerializer().Serialize(plist) + "}");
//return "{\"total\":2,\"rows\":" + new JavaScriptSerializer().Serialize(plist) + "}";
}
问题解决了
gridview正常显示了
在这里webservice的返回结果不再是soap,而是普通的json字符串
.net webservice非soap格式输出
最新推荐文章于 2023-12-19 17:31:06 发布