Ajax调用.Net并发送回cookie [英] Ajax call to .Net and sending cookie back

查看:38
本文介绍了Ajax调用.Net并发送回cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从html页面(移动设备)向.net服务器进行ajax调用,以进行登录和身份验证.现在,我正在以成功:true返回一个JSON响应.一切正常,但我需要设置cookie,以便在登录后进行其他呼叫以记录数据时记住该用户.

I am making an ajax call from an html page (mobile) to a .net server for a login and authentication. Right now i am sending back a JSON response with success:true. This all works fine but I need to set the cookies so that the user is remembered when I make other calls to record data once logged in.

我读过有关使用JSONP的信息,但是我不想走那条路,因为这意味着要进行很多更改,因此不必这样做.我只想在响应中发送回cookie并在客户端手动进行设置.

I read about using JSONP, but I would rather not go that route if I don't have to since it means changing a lot. I would like to just send the cookie back in the response and set it manually on the client side.

如何在.net服务器端检索此cookie(或创建cookie?",然后将其发送回响应中?

How do I retrieve this cookie (or create the cookie?" on the server side in .net and send it back in the response?

推荐答案

在ajax的成功事件中,您可以在客户端设置cookie

In your success event of ajax, you can set the cookie in client side

假设您的 JSON 就是这样

  {    "success": "true",    "username": "scott"   }

然后在您的ajax函数中,检查JSON,如果成功项目值为true,则设置cookie.

And in your ajax function, check the JSON and if the success item value is true, set the cookie.

$.ajax({
        url: "someserverpage.aspx",
        success: function(data) {
           if(data.success=="true")
           {
              SetCookie("YoursiteUsername",data.username,365);                   
           }
        }
});

SetCookie函数设置cookie.

The SetCookie function sets the cookie.

function SetCookie(c_name,value,exdays)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie=c_name + "=" + c_value;
}

这篇关于Ajax调用.Net并发送回cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆