【问题标题】:How to retrieve a session value from a generic handler, and use that value in IF statement of jquery?如何从通用处理程序中检索会话值,并在 jquery 的 IF 语句中使用该值?
【发布时间】:2019-09-25 23:10:01
【问题描述】:

我想从通用处理程序中检索一个会话值Session["somename"],然后在 jquery if 条件中使用该值。

我已经查看了一些与此问题相关的问题,但我还没有找到答案。代码在 #div1 上不显示任何内容,这意味着 result.Value 为空。我不知道可能没有检索到会话值。

public class ReadSession: IHttpHandler,
System.Web.SessionState.IRequiresSessionState {
    public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.Write(new JavaScriptSerializer().Serialize(new {
            Key = context.Request["key"],
            Value = context.Session[context.Request["key"]]
        }));
    }
}
$.ajax({
    url: '/ReadSession.ashx',
    method: 'post',
    dataType: 'json',
    key: 'Somename',
    success: function (result) {
        if (result.Value != null)
        {
            $('#div1').append('<li>' + encodedMsg + '</li>');
        }
    },
    error: function (error) {
        alert('error');
    }
})

如果 Result.Value != null,我希望 jquery 在 #div1 上显示一条消息。

【问题讨论】:

  • 结果如何?

标签: c# jquery ajax json.net session-state


【解决方案1】:

我认为你需要这个:

$.ajax({
    url: '/ReadSession.ashx',
    method: 'post',
    dataType: 'json',
    key: 'Somename',
    success: function (result) {
                  $('#div').append('<li><strong>' + result.Value + '</li>');
}
    },
    error: function (error) {
        alert('error');
    }
})

【讨论】:

  • 它在#div 上显示 null 但不显示会话的值。
猜你喜欢
  • 2017-05-07
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
相关资源
最近更新 更多