Home > php教程 > PHP开发 > body text

Analysis of AJAX return ajaxReturn() method in thinkphp

高洛峰
Release: 2016-12-30 09:45:18
Original
1726 people have browsed it

This article analyzes the AJAX return ajaxReturn() method in thinkphp. Share it with everyone for your reference, the details are as follows:

The system supports any AJAX class library. The Action class provides the ajaxReturn method for returning data to the client after an AJAX call. It also supports JSON, XML and EVAL three ways to receive data from the client, which can be set by configuring DEFAULT_AJAX_RETURN. The default configuration uses JSON format to return data. You can use different ways to return data when choosing different AJAX class libraries.

If you want to use ThinkPHP's ajaxReturn method to return data, you need to comply with certain return data format specifications. The data format returned by ThinkPHP includes:

status operation status
info prompt information
data return data

$this->ajaxReturn(返回数据,提示信息,操作状态);
Copy after login

return data data can support strings and numbers Arrays and objects are encoded and transmitted according to different return formats when returned to the client. If it is in JSON format, it will be automatically encoded into a JSON string. If it is in XML mode, it will be automatically encoded into an XML string. If it is in EVAL mode, only the string data data will be output, and status and info information will be ignored.

The following is a simple example:

$User=M("User");//实例化User对象
$result = $User->add($data);
if ($result){
  //成功后返回客户端新增的用户ID,并返回提示信息和操作状态
  $this->ajaxReturn($result,"新增成功!",1);
}else{
  //错误后返回错误的操作状态和提示信息
  $this->ajaxReturn(0,"新增错误!",0);
}
Copy after login
$data['status'] = 1;
$data['info'] = 'info';
$data['size'] = 9;
$data['url'] = $url;
$this->ajaxReturn($data,'JSON');
Copy after login

thinkphp source code:

/**
* Ajax方式返回数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param String $type AJAX返回数据格式
* @return void
*/
protected function ajaxReturn($data,$type='') {
    if(func_num_args()>2) {// 兼容3.0之前用法
      $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') {
      // 返回JSON数据格式到客户端 包含状态信息
      header('Content-Type:text/html; charset=utf-8');
      exit(json_encode($data));
    }elseif(strtoupper($type)=='XML'){
      // 返回xml格式数据
      header('Content-Type:text/xml; charset=utf-8');
      exit(xml_encode($data));
    }elseif(strtoupper($type)=='EVAL'){
      // 返回可执行的js脚本
      header('Content-Type:text/html; charset=utf-8');
      exit($data);
    }else{
      // TODO 增加其它格式
    }
}
Copy after login

Hope What this article describes will be helpful to everyone in PHP programming based on the ThinkPHP framework.

For more articles related to the analysis of the AJAX return ajaxReturn() method in thinkphp, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template