问题是当我尝试使用以下任何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引用异常。 那么男人的问题是如何解决它?