Different front-end and back-end cookies under second-level domain names, background C # errors that are easy to make when calling front-end JS Methods

Source: Internet
Author: User

Because on the register page, click Register to complete the following tasks:

① Add the registration information to the database through the background,

② "Registration successful" is displayed on the front-end ",

③ Write the registered name and gender into the cookie.

At first, I thought everything was done in the background, but I found a problem. The cookie generated in the background was in the first-level domain name. What I did was in the second-level domain name, and I needed to modify it in the background, I found a lot of information but could not succeed.

 

The background code is as follows:

 

// Set the condition 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 button#click (Object sender, eventargs E)
{
Username = textbox1.text;
Setwherecookie (username );
Response. Write ("<SCRIPT> If (confirm ('registration successful, you will log on to the homepage! ') Window. Location. href ='./Homepage. aspx '; </SCRIPT> ");
}

 

 

However, when a cookie is written on the front end, the second-level domain name can be taken directly in the domain, but the second-level domain name cannot be written on the background.

Cookie file generated at the front end
Sex
2
Localhost/jingsystem/
1600
1488151808
30066602
237686608
30066552
*
Username
Sssssaaaqw
Localhost/jingsystem/
1600
88282624
30066606
3136544720
30066555
*
Cookie file generated in the background
Sex
2
Localhost/
1600
1488151808
30066602
237686608
30066552
*
Username
Sssssaaaqw
Localhost/
1600
88282624
30066606
3136544720
30066555
*

After finding a solution, add the path in the background cookie code to control the cookie. Path = "/jingsystem/", and finally solve the problem.

 

Another solution is to write the method at the front end and call it at the backend.

The front-end code is as follows:

 

Function setcookie (name, value) // two parameters: one is the name of the cookie and the other is the value.
{
VaR days = 0.25; // This cookie will be saved for 30 days
VaR exp = new date (); // new date ("December 31,999 8 ");
Exp. settime (exp. gettime () + days x 24x60*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 );

}
}

 

 

 

Background code:

 

Protected void button#click (Object sender, eventargs E)
{
Username = textbox1.text;
Clientscript. registerstartupscript (clientscript. GetType (), "myscript", "<SCRIPT> writecookie (); If (confirm ('registered successfully, you will log on to the homepage! ') Window. Location. href ='./Homepage. aspx '; </SCRIPT> ");
Response. Write ("<SCRIPT> If (confirm ('registration successful, you will log on to the homepage! ') Window. Location. href ='./Homepage. aspx '; </SCRIPT> ");
}

 

 

If the previous sentence is not executed, You have to modify the background Code as follows:

Clientscript. registerstartupscript (clientscript. GetType (), "myscript", "<SCRIPT> writecookie (); If (confirm ('registered successfully, you will log on to the homepage! ') Window. Location. href ='./Homepage. aspx '; </SCRIPT> ");

 

Finally!

There are several questions:

① Why can the cookie written by the front-end directly carry a second-level domain name in the domain, and the path must be added for background writing.

② Clientscript. registerstartupscript and response. Write are written before and after, and are not executed before.

 

 

 

 

In addition, we recommend three good articles found in solving the problem:

A very practical class in the background of a high handwriting: http://blog.csdn.net/zhoufoxcn/archive/2008/04/21/2312440.aspx

Summary of front-end code for server call: http://www.cnblogs.com/lhuser/articles/1458825.html

Asp.net JS and C # for mutual access: http://www.cnblogs.com/ywqu/archive/2009/01/08/1371483.html

Explanation of cookie properties http://www.webdn.com/web_file/program/asp/N0610925/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.