Home >Web Front-end >JS Tutorial >jQuery AJAX timeout timeout emergency handling method

jQuery AJAX timeout timeout emergency handling method

php中世界最好的语言
php中世界最好的语言Original
2018-04-23 17:18:384750browse

This time I will bring you jQuery AJAX timeout emergency processing method, jQuery AJAX timeout emergency processing What are the precautions, the following is a practical case, let's take a look.

Let me first analyze the reasons for the timeout:

1. The network is not smooth.

2. The background operation is relatively slow (it is easy to occur when the server is running for the first time)

Timeout result: timeout in JQ sets the request timeout.

If the server response time exceeds the set time, it will enter ERROR (Error handling)

Timeout solution:

1. The default timeout is 0, which means it will never time out.

2. Try to set the timeout as large as possible. Disadvantages: Slows down the loading of data (seemingly. Haha).

3. Write a function about timeout processing in the ERRORcallback function: for example, you can call the data fetching function again in the case of timeout.

-------------------------------------------------- ---------------------------------------------------- ----------------

There is a success function in this ajax. It returns success and there is also a return function called error

This function will be triggered if the request fails.

I think you can handle it in the error.

If this function is executed, then you can call your second ajax method.

The timeOut timeout setting and event event processing of jQuery and ExtJS.

The timeOut timeout setting and event event processing of jQuery and ExtJS.

How to customize the timeout period for ajax requests, And handle the corresponding timeout event?

For jQuery, the timeout can directly set the timeout parameter and capture the second parameter in the error event. If it is "timeout", it means that the timeout event is captured, which is very clear.

Example:

$.ajax({
type: "POST"
,
contentType: "application/json"
,
url: "../ws/MyService.asmx/test"
,
data: '{"email":"'
+email+'"}'
, 
timeout: 30000, //超时时间:30秒
dataType: 'json'
,
error: function
(XMLHttpRequest, textStatus, errorThrown){
//TODO: 处理status, http status code,超时 408
// 注意:如果发生了错误,错误信息(第二个参数)除了得到null之外,还可能
//是"timeout", "error", "notmodified" 和 "parsererror"。
}, 
success: function
(result) {
// TODO: check result
}
});

In addition, the first parameter XMLHttpRequest returned by the error event has some useful information:

XMLHttpRequest.readyState: Status code

0 - (Uninitialized) The send() method has not been called yet

1 - (Loading) The send() method has been called and the request is being sent

2 - (Loading completed) send( ) method execution is completed and all response content has been received

3 - (Interaction) The response content is being parsed

4 - (Complete) The response content parsing is completed and can be called on the client

XMLHttpRequest.status attribute: some error codes

HTTP

1xx-Information Tips

These status codes indicate temporary responses. The client should be prepared to receive one or more 1xx responses before receiving a regular response.

100-Continue.

101-Switch protocol.

2xx-Success

This type of status code indicates that the server successfully accepted the client request.

200-OK. The client request was successful.

201-Created.

202-Accepted.

203-Non-authoritative information.

204-No content.

205-Reset content.

206-Part of the content.

3xx-Redirect

The client browser must take additional action to fulfill the request. For example, the browser may have to request a different page on the server or repeat the request through a proxy server.

301 - The object has been permanently moved, i.e. permanently redirected.

302 - The object has been temporarily moved.

304-Not modified.

307 - Temporary redirect.

4xx-Client Error

An error occurred and there appears to be a problem with the client. For example, the client requests a page that does not exist, and the client does not provide valid authentication information. 400 - Bad request.

401-Access Denied. IIS defines many different 401 errors, which indicate more specific error causes. These specific error codes show up in the browser but not in the IIS logs:

401.1 - Login failed.

401.2 - Server configuration caused login failure.

401.3 - Not authorized due to ACL restrictions on the resource.

401.4 - Filter authorization failed.

401.5-ISAPI/CGI application authorization failed.

401.7 – Access is denied by the URL authorization policy on the web server. This error code is specific to IIS6.0.

403-Forbidden: IIS defines a number of different 403 errors, which indicate more specific error causes:

403.1-Execution access is forbidden.

403.2 - Read access prohibited.

403.3 - Write access prohibited.

403.4 - SSL required.

403.5 - SSL128 required.

403.6 - IP address rejected.

403.7 - Client certificate required.

403.8 - Site access denied.

403.9-Too many users.

403.10-Invalid configuration.

403.11-Password change.

403.12 - Access to the mapping table is denied.

403.13 - Client certificate revoked.

403.14 - Directory listing denied.

403.15 - Client access permission exceeded.

403.16 - The client certificate is not trusted or invalid.

403.17 - The client certificate has expired or is not yet valid.

403.18 - The requested URL cannot be executed in the current application pool. This error code is specific to IIS6.0.

403.19 - CGI cannot be executed for clients in this application pool. This error code is specific to IIS6.0.

403.20-Passport login failed. This error code is specific to IIS6.0.

404-Not Found.

404.0-(None) – File or directory not found.

404.1 - The web site cannot be accessed on the requested port.

404.2-Web services extension locking policy blocks this request.

404.3-MIME mapping policy blocks this request.

405 - The HTTP verb used to access this page is not allowed (method not allowed)

406 - The client browser does not accept the MIME type of the requested page.

407 - Proxy authentication required.

412 - Precondition failed.

413 – The request entity is too large.

414-The request URI is too long.

415 – Unsupported media type.

416 – The requested range cannot be satisfied.

417 – Execution failed.

423 – Locking error.

5xx-Server Error

The server was unable to complete the request because it encountered an error.

500 - Internal server error.

500.12 - The application is busy restarting on the web server.

500.13-The web server is too busy.

500.15 - Direct request to Global.asa is not allowed.

500.16 – The UNC authorization credentials are incorrect. This error code is specific to IIS6.0.

500.18–URL authorization store cannot be opened. This error code is specific to IIS6.0.

500.100 - Internal ASP error.

501 - The header value specifies an unimplemented configuration.

502 - The web server received an invalid response while acting as a gateway or proxy server.

502.1-CGI application timed out.

502.2-CGI application error. application.

503 - Service is unavailable. This error code is specific to IIS6.0.

504-Gateway timeout.

505-HTTP version is not supported.

FTP

1xx - Positive Initial Reply

These status codes indicate that an operation has started successfully, but the client would like another reply before proceeding with the new command .

110 Restart mark reply.

120 Service is ready and will start in nnn minutes.

125 Data connection is open and transmission is starting.

150 The file status is normal and the data connection is ready to be opened.

2xx-Positive Completion Reply

An operation has been completed successfully. Clients can execute new commands. 200 command OK.

202 Command not executed, too many commands on the site.

211 system status, or system help reply.

212 Directory status.

213 File status.

214Help message.

215NAME system type, where NAME is the official system name listed in the AssignedNumbers document.

220 The service is ready to execute the new user's request.

221 Service closed the control connection. If appropriate, log out.

225 Data connection open, no transfer in progress.

226Close the data connection. The requested file operation was successful (for example, transferring the file or discarding the file).

227 Enter passive mode (h1, h2, h3, h4, p1, p2).

230 The user has logged in, continue.

250 The requested file operation is correct and completed.

257 "PATHNAME" has been created.

3xx - Positive Intermediate Reply

The command was successful, but the server needs more information from the client to complete processing the request. 331 The username is correct and a password is required.

332An account login is required.

350 The requested file operation is waiting for further information.

4xx - Transient Negative Completion Reply

The command was unsuccessful, but the error is temporary. If the client retries the command, it may succeed. 421 Service Unavailable, closing the control connection. This response will be sent to any command if the service determines that it must shut down.

425 Unable to open data connection.

426Connectionclosed;transferaborted.

450 The requested file operation was not performed. The file is unavailable (for example, the file is busy).

451 The requested operation terminated abnormally: A local error is being processed.

452 The requested operation was not performed. There is not enough system storage space.

5xx - Permanent Negative Completion Reply

The command was unsuccessful and the error is permanent. If the client retries the command, the same error will appear again. 500Syntax error, the command cannot be recognized. This may include errors such as the command line being too long.

501 There is a syntax error in the parameter.

502 Command not executed.

503 Bad command sequence.

504 The command with this parameter was not executed.

530 Not logged in.

532An account is required to store files.

550 The requested operation was not performed. The file is not available (e.g., file not found, no access rights).

551 The requested operation terminated abnormally: Unknown page type.

552 The requested file operation terminated abnormally: Storage allocation exceeded (for the current directory or data set).

553 The requested operation was not performed. Filename not allowed.

Common FTP status codes and their causes

150-FTP uses two ports: 21 for sending commands and 20 for sending data. Status code 150 means that the server is preparing to open a new connection on port 20 to send some data.

226 - Command opens a data connection on port 20 to perform operations such as transferring files. The operation completed successfully and the data connection was closed.

230 - This status code is displayed after the client sends the correct password. It means the user has logged in successfully.

331-This status code is displayed after the client sends the username. This status code will be displayed regardless of whether the username provided is a valid account on the system.

426 - The command opened a data connection to perform an operation, but the operation was canceled and the data connection was closed.

530 - This status code means that the user cannot log in because the username and password combination is invalid. If you are logging in with a user account, you may have typed the wrong username or password, or you may have selected to allow only anonymous access. If you log in using an anonymous account, IIS may be configured to deny anonymous access.

550-命令未被执行,因为指定的文件不可用。例如,要GET的文件并不存在,或试图将文件PUT到您没有写入权限的目录。

ExtJS 的默认超时时间是30s,超过就会ajax请求失败,http status code 408。

设置ExtJS的超时时间方法需要用Ext.data.Connection 对象,并捕捉requestexception事件,例子:

var
conn=new
Ext.data.Connection({
url: "../ws/MyService.asmx/test"
,
timeout : 60000, //自定义超时时间,这里是60秒 (默认30s)
autoAbort : false
,
disableCaching : true
,
method : "GET"
});
var
proxy = new
Ext.data.HttpProxy(conn);
proxy.getConnection().on("requestcomplete"
, function
(sender, response, options){ 
//成功,response.status = 200, response.statusText = 'OK'
});
proxy.getConnection().on("requestexception"
, function
(sender, response, options){
//异常,捕捉 esponse.status ( http status code 代码 )和 response.statusText 
});
var
store = new
Ext.data.Store({
proxy: proxy, 
reader: myReader, //需要另外定义一个reader
baseParams: {myargument:'myargumentValue'
}, //改成你的参数名和值
remoteSort: false
}); 
store.load();

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

asp.net+jquery.form做出图片异步上传功能

jquery删除table选中行

The above is the detailed content of jQuery AJAX timeout timeout emergency handling method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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