Generally, the types of information that will be sent when processing Ajax responses include data, success Information, error information, failure information, and processing status. the types of information transmitted are inconsistent. in addition to data, we usually want to pass the processing status. in this case, most of the options are JSON... generally, the types of information that will be sent when processing Ajax responses include data, success Information, error information, failure information, and processing status. the types of information transmitted are inconsistent. in addition to data, generally, you want to pass the processing status. in this case, the two information will be transmitted in JSON format. below are several common formats:
However, the execution status and Operation behavior can be used as an induction to differentiate the following transmission results:
{Code: 1, msg: "OK"} {success: true, result: "data", errorMsg: ""} {status: 'Success ', result: [], errorMsg :""}//...
However, the execution status and Operation behavior can be used as an induction to differentiate the following return results:
HTTP Method successful data operation error/failure
Read (Read) GET data error/failure information
Add (Create) POST success message error/failure message
Update)
Delete)
From the above induction, we can see the regularity. as long as there is a way to transfer the processing status and distinguish the types of data, there are actually a lot of simple, HTTP Status Codes is used to pass the HTTP processing Status. if you use this method to pass custom processing Status, HTTP Content can simply pass data, the data format is not limited to JSON, but other formats (text, xml, html) can also be used, and XMLHttpRequest itself has the ability to process HTTP Status Codes, while jQuery. ajax also provides the processing of error status, so you can use this to define the processing of the Status. There are several defined statuses in HTTP status Codes, which are suitable for passing the processing Status information:
Bad Request errors are applicable to form content errors. if the required field is not filled, the Email format is incorrect.
403Forbidden has no permissions. prohibited applications do not have logon or insufficient permissions.
500 Internal Server Error applicable to program errors
The following is an example of how jQuery receives information:
$.ajax({ type: "POST" , url: document.location, success: function (data, textStatus, jqXHR) { alert(data); }, error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR.responseText); } });
The code for passing error messages in PHP is as follows:
If (php_sapi_name () = 'CGI ') {header ("Status: 400 Bad Request");} else {header ("HTTP/1.0 400 Bad Request ");} exit ("storage failed !! ");
C # example of passing error messages by MVC:
Response. TrySkipIisCustomErrors = true; Response. StatusCode = 400; return Content ("storage failed !! ");
Article link:
Save this article for favorites!