JavaScript version of Struts2-Ajax integration

1. The role of Ajax: it can interact with the background server in an asynchronous way without reloading the page

2. Struts2-Json jar package (including Alibaba's own jar package)

commons-beanutils-1.7.0.jar

commons-collections-3.2.1.jar

commons-httpclient-3.1.jar

commons-jexl-2.1.1.jar

ezmorph-1.0.3.jar

fastjson-1.1.40.jar

gson-2.3.1.jar

json-lib-2.4-jdk15.jar

struts2-json-plugin-2.2.3.jar

2. Build your own Struts2 environment and configuration file filter

3. Front-end page -->




















//For all modern browsers, including IE5 and IE6, check if the browser supports the XMLHttpRequest object.
// If supported, create an XMLHttpRequest object. If not supported, create an ActiveXObject:
if(window.XMLHttpRequest){
// IE7+, Firefox, Chrome, Opera, Safari browsers execute the code
xmlhttp=new XMLHttpRequest();
}else{
// IE6, IE5 browsers execute the code
xmlhttp =new ActiveXObject("Microsoft.XMLHTTP");
}
//In the onreadystatechange event, we specify the tasks to be performed when the server response is ready to be processed.
//When readyState is equal to 4 and the status is 200, it means the response is ready:
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
//xmlhttp.responseText is executed in the background The data returned after completing the corresponding logic
document.getElementById("check_content").innerHTML=xmlhttp.responseText;

}
}
//The method of sending the request and the url and whether it is asynchronous or not
xmlhttp.open("get","usertest.action?username="+username+"&password="+password,true);
//发送请求
xmlhttp.send();
}
</script>

Precautions:

属性 描述
onreadystatechange 存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。
readyState

存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。

  • 0: 请求未初始化
  • 1: 服务器连接已建立
  • 2: 请求已接收
  • 3: 请求处理中
  • 4: 请求已完成,且响应已就绪
status 200: "OK"
404: 未找到页面

The status codes commonly used by the server and their corresponding meanings are as follows:

100 - Client must continue making requests

101 - The client asks the server to convert the HTTP protocol version according to the request

200 - successful transaction

201 - Prompt to know the URL of the new file

202 - Accepted and processed, but processing not completed

203 - Return information is uncertain or incomplete

204 - The request was received, but the return information was empty

205 - The server has completed the request, the user agent MUST reset the currently viewed file

206 - The server has completed the GET request of some users

300 - The requested resource is available in multiple places

301 - delete request data

302 - Request data found at other address

303 - Advise customers to visit other URLs or access methods

304 - The client has performed a GET, but the file has not changed

305 - The requested resource must be obtained from the address specified by the server

306 - code used in the previous version of HTTP, no longer used in the current version

307 - Declare the temporary deletion of the requested resource

400 - Bad request, such as syntax error

401 - Request authorization failed

402 - keep a valid ChargeTo header response

403 - request not allowed

404 - No file, query or URL found

405 - The method defined by the user in the Request-Line field is not allowed

406 - According to the Accept drag sent by the user, the requested resource is not accessible

407 - Similar to 401, the user must first be authorized on the proxy server

408 - The client did not complete the request within the time specified by the user

409 - The request cannot be completed for the current resource state

410 - The resource is no longer on the server and there is no further reference

411 - The server rejected the request for the user-defined Content-Length attribute

412 - One or more request header fields are incorrect in the current request

413 - The requested resource is larger than the size allowed by the server

414 - The requested resource URL is longer than the server allows

415 - The requested resource does not support the requested item format

416 - The request contains the Range request header field, there is no range indication value within the current request resource range, and the request does not contain the If-Range request header field

417 - The server does not meet the expected value specified by the request Expect header field. If it is a proxy server, it may be that the next-level server cannot meet the request.

500 - The server generated an internal error

501 - The server does not support the requested function

502 - The server is temporarily unavailable, sometimes to prevent system overload

503 - Server overloaded or suspended for maintenance

504 - The gateway is overloaded, the server uses another gateway or service to respond to the user, and the waiting time is set longer.

505 - The server does not support or refuses to support the HTTP version specified in the request header

4、

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324802723&siteId=291194637