An example of _php analysis in thinkphp Ajaxreturn () method of Ajax return

Source: Internet
Author: User
Tags json php template codeigniter zend framework

This paper analyzes the Ajax Return Ajaxreturn () method in thinkphp. Share to everyone for your reference, specific as follows:

The system supports any AJAX class library, and the action class provides a Ajaxreturn method for Ajax calls to return data to the client. It also supports JSON, XML, and eval three ways to accept data to clients, set by configuring Default_ajax_return, and the default configuration returns data in JSON format, which can be returned in different ways when choosing a different AJAX class library.

To return data using the thinkphp Ajaxreturn method, you need to follow certain formatting specifications for the returned data. The data formats returned by thinkphp include:

Status Action State
Info Tip Info
Data back

$this->ajaxreturn (returning data, prompting information, operating status);

The return data can support strings, numbers and arrays, and objects, which are encoded and transmitted according to different return formats when returned to the client. If it is in JSON format, it is automatically encoded as a JSON string, and if it is XML, it is automatically encoded into an XML string, and if it is the Eval method, it will only output string data and ignore status and info information.

The following is a simple example:

$User =m ("user"),//Instantiate User object
$result = $User->add ($data);
if ($result) {
  ///Success Returns the client new user ID, and returns the prompt information and operation status
  $this->ajaxreturn ($result, "New success!" ", 1);
} else{
  ///Error returns incorrect operation status and prompt information
  $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:

/** * Ajax mode returns data to client * @access protected * @param mixed $data The data to be returned * @param String $type ajax return data format * @return void */P rotected function Ajaxreturn ($data, $type = ') {if (Func_num_args () >2) {//compatible before 3.0 usage $args = Func_get_arg
      S ();
      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 ') {//returns the JSON data format to the client containing the state information header (' content-type:text/html; Charset=utf-8 ');
    Exit (Json_encode ($data));
      }elseif (Strtoupper ($type) = = ' xml ') {//return header of data in XML format (' Content-type:text/xml; Charset=utf-8 ');
    Exit (Xml_encode ($data));
      }elseif (Strtoupper ($type) = = ' EVAL ') {//Return executable JS script header (' content-type:text/html; Charset=utf-8 ');
    Exit ($DATA); }else{//TODO Add other format}}

 

More interested readers of thinkphp related content can view the website topic: "thinkphp Introductory Course", "thinkphp template Operation Skill Summary", "thinkphp Common Method Summary", "The CodeIgniter Introductory Course", "CI ( CodeIgniter) Framework Advanced tutorials, introductory tutorials for the Zend framework Framework, Smarty Templates Primer tutorial, and PHP template technology summary.

I hope this article will help you with the PHP program design based on thinkphp framework.

Related Article

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.