JavaScript Study Notes-client-to-Server Communication

Source: Internet
Author: User
To interact between the client and the server in a Web project, you can use cookies, hidden frameworks, HTTP requests, LiveConnect requests, and smart HTTP requests. cookiecookie is the first client-server interaction method that JavaScript can use. Browser... SyntaxHighligh

In a Web project, you can use cookies, hidden frameworks, HTTP requests, LiveConnect requests, and smart HTTP requests to achieve client-to-server interaction,

1. cookie

Cookie is the first client-server interaction method that JavaScript can use. When a browser sends a request to the server, the cookie stored for the server will be sent together with other information to the server. This allows JavaScript to set a cookie on the client, and then the server can read it.

1. cookie Components

Name-each cookie is represented by a unique name. The name can contain letters, numbers, and underscores. Case Insensitive.

Value -- the string value stored in the cookie. This value must be encoded with encodeURIComponent () before being stored to avoid data loss or cookie occupation. The sum of names and values cannot exceed 4095 bytes, that is, 4 kb.

Domain-in security considerations, the website cannot access cookies created by other domains. After a cookie is created, the domain information is stored as part of the cookie. However, this setting can be overwritten to allow another website to access the cookie.

Path-the security feature of another cookie. The path limits access to a specific directory on the Web server. For example, you can specify that the cookie can only be renewed from http://www.wrox.com/books.

Expiry date: determines when the cookie is deleted. By default, when the browser is closed, the cookie will be deleted, but you can set the deletion time on your own. This value is a Date in GMT format (you can use the toGMTString () method of the Date object), used to determine the exact time when the cookie should be deleted. If the set date is the time before the current date, the cookie is immediately deleted.

Security Flag-Indicates whether a cookie can only be accessed from a secure website (a website using SSL and https protocols. You can set this value to true to enhance protection and prevent cookies from being accessed by other websites.

2. Other security restrictions

I) each domain can store up to 20 cookies on a single user's machine;

Ii) the total size of each cookie cannot exceed 4096 bytes;

Iii) the total number of cookies on a user's machine cannot exceed 30.

3. cookie in JavaScript

The following is a function for setting cookies:

Function setCookie (sName, sValue, oExpires, sPath, sDomain, bSecure ){
Var sCookie = sName + "=" + encodeURIComponent (sValue );
If (oExpires ){
SCookie + = "; expires =" + oExpires. toGMTString ();
}
If (sPath ){
SCookie + = "; path" + sPath;
}
If (sDomian ){
SCookie + = "; domain" + sDomain;
}
If (bSecure ){
SCookie + = "; secure ";
}
Document. cookie = sCookie;
}

The setCookie () function is only required by the first two parameters. The function call method is as follows:

SetCookie ("name", "Honey fruit ");

SetCookie ("book", "JavaScript advanced programming", new Date (Date. parse ("Jan 1, 2006 ")));

SetCookie ("message", "hello", new Date (Date. parse ("Jan 1, 2006"), "/books", http://www.wrox.com, true );

The following function is a method for obtaining a cookie Based on the cookie name. The Code is as follows:

Function getCookie (sName ){
Var sRE = "(? :;.)? "+ SName +" = ([^;] *);?;
Var oRE = new RegExp (sRE );
If (oRE. test (document. cookie )){
Return decodeURIComponent (RegExp ["$1 ");
} Else {
Return null;
}
}

 

You can call this method to obtain the cookie with the specified name. The call example is as follows:

Var sName = getCookie ("name ");

Next, we will write a function to delete the cookie. You only need to set the expiration time to the previous time. The Code is as follows:

Function deleteCookie (sName, sPath, sDomain ){

SetCookie (sName, "", new Date (0), sPath, sDomain );

}

 

4. server-side cookies

1) JSP

Jsp provides a very simple interface for processing cookies. The request object is automatically initialized when the JSP is executed. There is a method to return a cookie object array, getCookies. Each Cookie object has methods such as getName (), getPath (), getDomain (), getSecure (), and getMaxAge (). The following describes how to obtain the Cookie:

Public static Cookie getCookie (HttpServletRequest request, String name ){

Cookie [] cookies = request. getCookies ();

If (cookies! = Null ){

For (int I = 0; I

If (cookies [I]. getName (). equals (name )){

Return cookies [I];

}

}

}

}

Next let's take a look at how to create a cookie:

Cookie nameCookie = new Cookie ("name", "Amigo ");

NameCookie. setDomain (http://www.wrox.com ");

NameCookie. setPath ("/books ");

Response. addCookie (nameCookie );

To delete a cookie, use the following method:

Cookie cookieToDelete = getCookie ("name ");

CookieToDelete. setMaxAge (0 );

Response. addCookie (cookieToDelete );

2) ASP. NET

.

3) PHP

.

 

Ii. Hide the framework

The method is to create a 0 pixel high framework that can communicate with the server using JavaScript. This communication method requires two parts: the JavaScript Object used for processing client communication and the special page for processing communication on the server. Eg.

Example of hiding a framework

In the first framework, two functions are defined, one for sending requests to the server and the other for processing responses. The request sending function is as follows:

Function getServerInfo (){

Parent. frames ["hiddenFrame"]. location. href = protected hiddenframecom.html ";

}

The code for processing the response function handleResponse () is as follows:

Function handleResponse (sResponseTextt ){

Alert ("server return:" + sResponseTextt );

}

The page for processing hidden requests must output a common HTML page, with oneElement that contains the returned element. UseYou can easily process multiple rows of data. This page must call the handleResponse () function in the main framework. The instance code is as follows: </p>

Example of hiding a framework

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.