How does PHP pass the state of success or failure of Ajax? HTTP Status Codes can implement

Source: Internet
Author: User
Tags json

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!!"    );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.