The types of information that are typically transmitted when handling AJAX responses are: data, success information, error information, failure information, and processing status, the type of information passed is inconsistent, plus, in addition to the data, usually want to pass the processing state, which most of the choice is to pass the two information in JSON, Here are a few common formats:
But with the execution state and operation behavior as a generalization, can distinguish the following delivery results:
{code:1, msg: "OK"} {success:true, Result: "Data", ErrorMsg: "} {Status: ' Success ', result: [], ErrorMsg: ""}//...
But with the execution state and operation behavior as an induction, you can distinguish the following kinds of return results:
Data manipulation HTTP Method success Error/failure
Read (read) Get data error/failure information
New (Create) POST Success Information error/failure message
Modify (Update)
Remove (delete)
From the above induction can see the regularity, and then as long as there are methods to transfer the processing of the state, and can distinguish between the types of data, in fact, a lot of simple, and HTTP status codes is used to pass HTTP processing state, if you use this way to pass the custom processing state, so that HTTP Content can be very simple to pass data, so that the data format is not limited to JSON, can also use other formats (text, XML, HTML), and XMLHttpRequest itself has the ability to handle HTTP Status codes, and Jquery.ajax also provides error status processing, so you can use this to define the processing of the state, which has several defined states in the HTTP status codes, and is ideal for passing information on the processing state:
400Bad Request error Application error in form content, such as required field not filled, Email format error
403Forbidden no permissions, prohibited application without login or insufficient permissions
500Internal Server error Internal server errors applicable to bugs in the program
Examples of jQuery receiving information
The code is as follows |
Copy Code |
$.ajax ({ Type: "POST", Url:document.location, Success:function (data, Textstatus, JQXHR) { alert (data); }, Error:function (JQXHR, Textstatus, Errorthrown) { alert (Jqxhr.responsetext); } }); |
Examples of PHP passing error messages
The code is as follows |
Copy Code |
if (php_sapi_name () = = ' cgi ') { Header ("status:400 bad Request"); } else { Header ("http/1.0 Request"); } Exit ("Store failure!!"); |
Examples of C # MVC passing error messages
The code is as follows |
Copy Code |
Response.tryskipiiscustomerrors = true; response.statuscode = 400; return Content ( "Save failed!!" ); |