Home > Web Front-end > JS Tutorial > body text

How to solve the inconsistent value of the constantly changing session in Ajax access and the difference between GET and POST in the HTTP protocol

亚连
Release: 2018-05-24 16:34:02
Original
1568 people have browsed it

This article mainly introduces the solution to the inconsistent value of the constantly changing session in Ajax access and the relevant information on the difference between GET and POST in the HTTP protocol. Friends who need it can refer to it

I am making one today I encountered a problem when using the progress bar. I stored a counter in the session. When a piece of data was crawled, the value was 1. Then the front desk obtained the value of the session every 3 seconds, but the problem came out. In FF Below, the values ​​obtained are normal, but under IE, they are the previous values. Only when the page is reopened can the latest session value be obtained:

The following is my proBar .jsp code:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<script type="text/javascript" src="<%=path %>/js/jquery.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.progressbar.min.js"></script>
<script type="text/javascript">
function createXMLHttpRequest() {
  var xmlHttp;
  if (window.ActiveXObject) {
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
   xmlHttp = new XMLHttpRequest();
  }
  return xmlHttp;
 }
 function btn_click() {
  var xmlHttp; 
  xmlHttp = createXMLHttpRequest();
  xmlHttp.onreadystatechange = processor;
  //xmlHttp.open("GET", "/jbbs/servlet/ControlCrawl?method=getinforsize", true);
  xmlHttp.open("POST", "/jbbs/servlet/ControlCrawl?method=getinforsize", true);
  xmlHttp.send(null);
  function processor() {
   .......
  }
 }
 var action=setInterval("btn_click();", );
 </script>
<span>爬取进度:</span><br/>
<p id="container"></p>
Copy after login

Later I changed GET to POST and it became normal.

PS: Regarding HTTP GET and POST

Http defines different methods of interacting with the server. There are 4 basic methods. They are GET, POST, PUT, and DELETE. The full name of URL is resource descriptor. We can think of it this way: a URL address is used to describe a resource on the network, and GET, POST, PUT, and DELETE in HTTP correspond to the search, modification, and addition of this resource. Delete 4 operations. At this point, everyone should have a general understanding. GET is generally used to obtain/query resource information, while POST is generally used to update resource information.

#1. According to the HTTP specification, GET is used for information acquisition and should be safe and idempotent.  

(1). The so-called safe means that the operation is used to obtain information rather than modify information. In other words, GET requests should generally not have side effects. That is to say, it only obtains resource information, just like a database query. It will not modify or add data, and will not affect the status of the resource.

* Note: The meaning of security here only refers to non-modified information.

(2). Idempotent means that multiple requests to the same URL should return the same result.​

Idempotence (idempotent, idempotence) is a mathematical or computer science concept that is commonly seen in abstract algebra.

Impotence has the following definitions:  

For unary operations, if an operation is performed multiple times on all numbers in the range, the result is If the result is the same as the result of performing the operation once, then we say the operation is idempotent. For example, absolute value arithmetic is an example. In the set of real numbers, there are abs(a)=abs(abs(a)).

For binocular operations, it is required that when the two values ​​​​participating in the operation are equal, if the operation result is equal to the two values ​​​​participating in the operation, the operation is said to be idempotent. For example, to find two The function of the maximum value of a number is idempotent in the set of real numbers, that is, max(x,x) = x.​

But in actual application, the above two regulations are not so strict. Examples of citing other people's articles: For example, the front page of a news site is constantly updated. Although the second request returns a different batch of news, the operation is still considered safe and idempotent because it always returns the current news. Basically, if the goal is that when the user opens a link, he can be sure that the resource has not changed from his perspective.​

2. According to the HTTP specification, POST represents a request that may modify resources on the server.

Continue to quote the above example: Take the news website as an example. Readers’ comments on the news should be implemented through POST, because the resources of the site are different after the comments are submitted, or It says that the resource has been modified.

But in actual practice, many people do not follow the HTTP specifications. There are many reasons for this problem, such as:

1. Many people are greedy for convenience and use GET when updating resources, because using POST must go to the FORM (form), which will be a little troublesome.

2. Adding, deleting, modifying, and checking resources can actually be completed through GET/POST, and there is no need to use PUT and DELETE.

3. Another is that the early Web MVC framework designers did not consciously treat and design URLs as abstract resources, which led to a more serious problem that the traditional Web MVC framework basically Only supports GET and POST HTTP methods, but does not support PUT and DELETE methods.​

* About MVC:

MVC originally existed in the Desktop program. M refers to the data model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation codes of M and V, so that the same program can use different representations.

The above three points typically describe the old style (which does not strictly adhere to the HTTP specification). With the development of the architecture, REST (RepresentationalState Transfer) now appears, a new style that supports the HTTP specification. There are not many here. Having said that, you can refer to "RESTful Web Services".

Let’s look at the difference between GET and POST from the surface:

(1). First of all, "the data submitted by GET can only be up to 1024 bytes". Because GET submits data through URL, the amount of data that can be submitted by GET is directly related to the length of the URL. In fact, there is no upper parameter limit for URLs, and the HTTP protocol specification does not limit URL length. This limit is imposed by specific browsers and servers. IE's limit on URL length is 2083 bytes (2K 35). For other browsers, such as Netscape, FireFox, etc., there is theoretically no length limit, and the limit depends on the support of the operating system. Note that this limit is the entire URL length, not just your parameter value data length.

(2). Theoretically, there is no size limit for POST, and the HTTP protocol specification does not impose a size limit. It is inaccurate to say that "the amount of POST data has a size limit of 80K/100K". POST data There is no limit. The limit is the processing power of the server's handler.

For ASP programs, the Request object has a data length limit of 100K when processing each form field. But if you use Request.BinaryRead, there is no such restriction.

Extended from this, for IIS 6.0, Microsoft has increased restrictions for security reasons.

We also need to pay attention to:

1). The default ASP POST data volume of IIS 6.0 is 200KB, and the limit of each form field is 100KB.  

2).The default maximum size of uploaded files in IIS 6.0 is 4MB.  

3).The default maximum request header of IIS 6.0 is 16KB.

There were no such restrictions before IIS 6.0.

So the 80K and 100K above may be just the default values ​​(note: I have not confirmed the parameters of IIS4 and IIS5 yet), but they can definitely be set by yourself. Since each version of IIS has different default values ​​for these parameters, please refer to the relevant IIS configuration documentation for details.

3. In ASP, the server uses Request.QueryString to obtain GET request parameters, and Request.Form to obtain POST request parameters.

In JSP, use request.getParameter(\"XXXX\") to obtain it. Although there is also a request.getQueryString() method in jsp, it is more troublesome to use, such as: passing A test.jsp?name=hyddd&password=hyddd, use request.getQueryString() to get: name=hyddd&password=hyddd. In PHP, you can use $_GET and $_POST to obtain data in GET and POST respectively, while $_REQUEST can obtain data in both GET and POST requests. It is worth noting that there are hidden dangers in using request in JSP and $_REQUEST in PHP. I will write an article summary next time.​

#4. POST is more secure than GET.

Note: The security mentioned here is not the same concept as the "security" mentioned in GET above. The meaning of "security" above is only that no data modification is made, and the meaning of security here is the true meaning of Security. For example: when submitting data through GET, the username and password will appear in clear text on the URL, because (1) the login page may be Browser cache, (2) other people view the browser history, then others can get your account and password. In addition, using GET to submit data may also cause Cross-site request forgery attacks.

To summarize, Get is a request to the server for data, while Post is a request to submit data to the server. In FORM (form), the Method defaults to "GET". In essence, GET and POST only have different sending mechanisms, not one is taken and the other is sent!

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Implementing file upload function based on Ajax and HTML5 in MVC

How to solve ajax browsing in Google Chrome Failed on the server

Based on ajax, click to load more without refreshing to load this page

##

The above is the detailed content of How to solve the inconsistent value of the constantly changing session in Ajax access and the difference between GET and POST in the HTTP protocol. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template