• cookie在二级域名下的前后台的不同,后台C#调用前台js方法容易犯的错误


    因为在做注册页面,点击注册按钮完成以下任务:

    ①把得到的注册信息,通过后台添加到数据库中,

    ②在前台显示“注册成功”,

    ③在注册的名字,性别写进cookie中

    刚开始想着所有的东西都在后台完成,但是发现一个问题,在后台产生的cookie是在一级域名中的,我做的在二级域名中,需要后台修改,查找了不少资料但是无法成功。

    后台代码如下:

     //设置条件Cookie  
        private void SetWhereCookie(string where)
        {
            HttpCookie cookie 
    = new HttpCookie("username");
            cookie.Value 
    = where;
            cookie.Expires 
    = DateTime.Now.AddDays(1d);        

            Response.Cookies.Add(cookie);
        }


    protected void Button1_Click(object sender, EventArgs e)
        {
     username 
    = TextBox1.text;
    SetWhereCookie(username );
    Response.Write(
    "<script> if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");
    }

     但是遇到前端写的cookie就可以在domain中直接带着二级域名,而后台写的却不可以。

    前台生成的cookie文件
    sex
    2
    localhost
    /jingsystem/
    1600
    1488151808
    30066602
    237686608
    30066552
    *
    username
    sssssaaaqw
    localhost
    /jingsystem/
    1600
    88282624
    30066606
    3136544720
    30066555
    *
    后台生成的cookie文件
    sex
    2
    localhost
    /
    1600
    1488151808
    30066602
    237686608
    30066552
    *
    username
    sssssaaaqw
    localhost
    /
    1600
    88282624
    30066606
    3136544720
    30066555
    *

    找到了一种解决办法,在后台的cookie代码中添加path来控制cookie.Path="/jingsystem/",总算把这个问题解决。

    还有一套解决方案是,前台写好方法,后台来调用。

    前端代码如下:

    function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
    {
    var Days 
    = 0.25//此 cookie 将被保存 30 天
    var exp   = new Date(); //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie 
    = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }

    function writeCookie()
      {
          alert(
    "hehe");
           SetCookie (
    "username", document.getElementById ("textusername").value); 
           var obj 
    = document.getElementsByName(RadioButtonList1); 
            
    for(i = 0;  i <obj.length;i++)    
            {         
    if(obj[i].checked)   SetCookie ("sex",i);       
                          
            }          
      }

    后台代码:

    protected void Button1_Click(object sender, EventArgs e)
        {
     username 
    = TextBox1.text;
    ClientScript.RegisterStartupScript(ClientScript.GetType(), 
    "myscript""<script>writeCookie();if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");                
    Response.Write(
    "<script> if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");
    }

    又遇见前一句不执行问题,所以只好修改后台代码如下:

     ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>writeCookie();if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");

    终于搞定!

    还有几个问题存在着疑惑:

    ①为什么前端写的cookie就可以在domain中直接带着二级域名,后台写的必需得加上path。

    ②ClientScript.RegisterStartupScript与response.write前后两句写,前面不执行。

     

    此外推荐三篇在解决问题中找到的好文章:

    某位高手写的后台一个很实用的类:http://blog.csdn.net/zhoufoxcn/archive/2008/04/21/2312440.aspx

    服务器调用前台代码的总结文章:http://www.cnblogs.com/lhuser/articles/1458825.html

    asp.net中js与c#为互相访问:http://www.cnblogs.com/ywqu/archive/2009/01/08/1371483.html

    有关cookie的属性讲解   http://www.webdn.com/web_file/program/asp/N0610925/


  • 相关阅读:
    浏览器内置对象及其方法
    Leetcode | Path Sum I && II
    Leetcode | 3Sum
    算法分析之渐近符号
    Leetcode | Two Sum
    Leetcode | Wildcard Matching
    LeetCode | Regular Expression Matching
    Leetcode | Subsets I & II
    Leetcode | Populating Next Right Pointers in Each Node I & II
    爱是恒久忍耐,又有恩慈
  • 原文地址:https://www.cnblogs.com/quanhai/p/1690153.html
Copyright © 2020-2023  润新知