Analysis of the ajaxReturn () method returned by AJAX in thinkphp, thinkphpajaxreturn

Source: Internet
Author: User

Analysis of the ajaxReturn () method returned by AJAX in thinkphp, thinkphpajaxreturn

This article analyzes the AJAX return ajaxReturn () method in thinkphp. We will share this with you for your reference. The details are as follows:

The system supports any AJAX class library. The Action class provides the ajaxReturn method to return data to the client after AJAX is called. The client can receive data in JSON, XML, and EVAL modes. You can configure DEFAULT_AJAX_RETURN to set the data. By default, data is returned in JSON format, you can use different methods to return data when selecting different AJAX class libraries.

If you want to use the ajaxReturn method of ThinkPHP to return data, you must follow the format specifications of the returned data. The data formats returned by ThinkPHP include:

Status operation status
Info prompt information
Data returned data

$ This-> ajaxReturn (return data, prompt information, operation status );

The returned data supports strings, numbers, arrays, and objects. The returned data is encoded and transmitted based on different return formats. If it is in JSON format, it is automatically encoded as a JSON string. If it is in XML format, it is automatically encoded as an XML string. If it is in EVAL format, only the string data is output, the status and info information are ignored.

The following is a simple example:

$ User = M ("User"); // instantiate the User object $ result = $ User-> add ($ data); if ($ result) {// after the client is successfully added, the new user ID is returned, and the prompt information and Operation Status $ this-> ajaxReturn ($ result, "added successfully! ", 1);} else {// operation status and prompt message $ this-> ajaxReturn (0," New error! ", 0 );}
$data['status'] = 1;$data['info'] = 'info';$data['size'] = 9;$data['url'] = $url;$this->ajaxReturn($data,'JSON');

Thinkphp source code:

/*** Return data to the client using Ajax Method * @ access protected * @ param mixed $ data the data to be returned * @ param String $ type AJAX return data format * @ return void */protected function ajaxReturn ($ data, $ type = '') {if (func_num_args ()> 2) {// usage before 3.0 compatibility $ args = func_get_args (); array_shift ($ args ); $ info = array (); $ info ['data'] = $ data; $ info ['info'] = array_shift ($ args ); $ info ['status'] = array_shift ($ args); $ data = $ info; $ type = $ args? Array_shift ($ args): '';} if (empty ($ type) $ type = C ('default _ AJAX_RETURN '); if (strtoupper ($ type) = 'json') {// return the header containing the status information ('content-Type: text/html; charset = UTF-8 ') from the JSON data format to the client '); exit (json_encode ($ data);} elseif (strtoupper ($ type) = 'xml') {// return the XML format data header ('content-Type: text/xml; charset = UTF-8 '); exit (xml_encode ($ data);} elseif (strtoupper ($ type) = 'eval ') {// return the executable js script header ('content-Type: text/html; charset = UTF-8 '); exit ($ data );} else {// Add other formats to TODO }}

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.