How does PHP pass the Ajax success or failure status? HTTPStatusCodes can be implemented

Source: Internet
Author: User
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!

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.