Sharepoint WebMethod Session始终为null

时间:2014-04-02 08:49:13

标签: jquery ajax sharepoint-2010

每个人都好。我有以下问题:

  1. 我有一些带有一些ajax的webpart
  2. 我有.ascx页面的webmethods。
  3. 问题是当我尝试使用以下任何JS函数时:

    function getSessionData() {
        $.ajax({
            type: 'POST',
            url: '/_layouts/MyProject/WebService.aspx/getSessionData',
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                // Do something interesting here.
                console.log(msg);
            }
            , error: function (xhr, ajaxOptions, thrownError) {
                console.log('error:');
                console.log(xhr.status);
                console.log(thrownError);
            }
        });
    }
    
    function setSessionData(str) {
        $.ajax({
            type: 'POST',
            url: '/_layouts/MyProject/WebService.aspx/setSessionData',
            data: "{'id': '" + str + "' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                // Do something interesting here.
                console.log(msg);
            }
            , error: function (xhr, ajaxOptions, thrownError) {
                console.log('error:');
                console.log(xhr.status);
                console.log(thrownError);
            }
        });
    }
    

    使用服务器端代码

        [WebMethod(EnableSession = true)]
        public static string getSessionData()
        {
            string wpsiid = "no data";
            try
            {
                wpsiid = HttpContext.Current.Session["wpsiid"].ToString();
            }
            catch (Exception)
            {
                wpsiid = "error occured";
                //throw;
            }
            return wpsiid;
        }
    
        [WebMethod(EnableSession = true)]
        public static string setSessionData(string id)
        {
            string ret = "ok";
            try
            {
                //HttpContext.Current.Session["wpsiid"] = id;
                HttpContext.Current.Session.Add("wpsiid",id);
            }
            catch (Exception)
            {
                ret = "error...";
                //throw;
            }
    
            return ret;
        }
    

    我总是得到null引用异常。 那么男人的问题是如何解决它?

0 个答案:

没有答案