做的不是Web Service,是ASP.NET网站,在CS页面里添加了using System.Web.Services;
这样就也可以用[WebMethod]了。
简单的返回个string什么的没问题 [WebMethod]
public static string TestFunc2(string str)
{
return "This is the returns of behind code function two!You give me the string :"+str;
}
function Button1_onclick() {
PageMethods.TestFunc2(function (value) {
var divMsg = document.getElementById("divMsg");
divMsg.innerHTML = value;
});
}
但是复杂的返回DataSet,报错。我看这里,改了一点,还是不行我改成这样
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Xml)]
public static DataSet GetDS()
{
return HttpContext.Current.Session["CurrentUser"] as DataSet;
}
也有的说改成
[WebMethod]
[System.Web.Script.Services.ScriptMethod()] public static DataSet GetDS()
{
return HttpContext.Current.Session["CurrentUser"] as DataSet;
}JS端代码
function Button1_onclick() {
PageMethods.GetDS(function (value) {
var divMsg = document.getElementById("divMsg");
divMsg.innerHTML = value.Tables[0].columns[0].name;
});
}
这样就也可以用[WebMethod]了。
简单的返回个string什么的没问题 [WebMethod]
public static string TestFunc2(string str)
{
return "This is the returns of behind code function two!You give me the string :"+str;
}
function Button1_onclick() {
PageMethods.TestFunc2(function (value) {
var divMsg = document.getElementById("divMsg");
divMsg.innerHTML = value;
});
}
但是复杂的返回DataSet,报错。我看这里,改了一点,还是不行我改成这样
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Xml)]
public static DataSet GetDS()
{
return HttpContext.Current.Session["CurrentUser"] as DataSet;
}
也有的说改成
[WebMethod]
[System.Web.Script.Services.ScriptMethod()] public static DataSet GetDS()
{
return HttpContext.Current.Session["CurrentUser"] as DataSet;
}JS端代码
function Button1_onclick() {
PageMethods.GetDS(function (value) {
var divMsg = document.getElementById("divMsg");
divMsg.innerHTML = value.Tables[0].columns[0].name;
});
}
解决方案 »
- 请教在showModalDialog窗口中下载文件的问题
- 模糊查询
- 给已有数据的DataTable加上一列,并将每行的值都设为1。怎么写?
- 如何给DateTime类型的变量赋一个空值?
- 如保在没数据的情况下也能让GridView的head部分显示出来?
- 请问大吓们可不可以在后台代码中动态生成服务器控件
- 缓存底层有锁吗?可以解决并发问题?有空闲的人员帮研究研究..
- 告急各位大师,做过.net报表的,知道Azure云服务的都进来帮个忙,谢谢了
- 【在线等待】如何给刷新时间赋值
- 用户控件中,怎么在ascx.cs中访问并修改对应ascx中<div id="id0" style="...">style中的一个值?
- asp.net问题
- 请教动态增加控件行
[WebMethod]不能访问Session
webservice...返回JSON格式还是好些
XML也行datatable 和 dataset 都有点。怪怪的
出现错误提示“WebMethod Dataset System.InvalidOperationException: A circular reference was detected......”
在bin里面导入ajax.dll
在webconfig中写
<httpHandlers>
<add path="ajax/*.ashx" verb="POST,GET" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
在 page_Load 中写 Ajax.Utility.RegisterTypeForAjax(typeof(PageMethods)); //注册页面 [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public DataSet xx()
{
//自己写方法
}js 调用 function getValue() { PageMethods.GetDS(callback_value);
} function callback_value(res) {
//这里的res.value 是你要返回的ds
}
Stream stream = new MemoryStream);
formatter.Serialize(stream, DS);
byte[]streams=stream;
string s=system..encode.utf8.getstring(streams);
return s;