做的不是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;
            });
        }

解决方案 »

  1.   

    把[WebMethod]改为这样试试  [WebMethod(EnableSession=true)]
    [WebMethod]不能访问Session
      

  2.   

    试了,不行,想也应该不行,因为这是Ajax.dll的做法。而WebMethod是用了ScriptManager,它的enabledviewstate已经是true了
      

  3.   


    webservice...返回JSON格式还是好些
      

  4.   


    XML也行datatable  和 dataset 都有点。怪怪的
      

  5.   

    返回强类型的数据集合,这样比较头脑清晰地编写程序,程序在编译时即可查出主要的设计错误(反过来说,这才叫设计,而DataSet那个可能就不是设计而是拼凑),不用等到崩溃了才开始叫唤。
      

  6.   

    在我发的那个链接里,那个人报的错,跟我的一样
    出现错误提示“WebMethod Dataset System.InvalidOperationException: A circular reference was detected......” 
      

  7.   

    哦,改成它写的之后,倒是不报那个错了,就是value始终为"未定义"
      

  8.   

    我以前做过类似的 
    在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
    }
      

  9.   

    IFormatter formatter = new BinaryFormatter();
    Stream stream = new MemoryStream);
    formatter.Serialize(stream, DS);
    byte[]streams=stream;
    string s=system..encode.utf8.getstring(streams);
    return s;