Home > Backend Development > PHP Problem > Is ajaxreturn a built-in method in php?

Is ajaxreturn a built-in method in php?

Guanhui
Release: 2023-02-28 21:36:01
Original
3172 people have browsed it

Is ajaxreturn a built-in method in php?

Is ajaxReturn a built-in method in php?

ajaxReturn is not a built-in method in php. AjaxReturn is the Action class in ThinkPHP that provides the ajaxReturn method for After the AJAX call, data is returned to the client, and JSON, XML and EVAL are supported for receiving data from the client. This is set by configuring DEFAULT_AJAX_RETURN. The default configuration uses JSON format to return data, which can be used when selecting different AJAX class libraries. Data is returned in different ways.

ajaxReturn usage

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, numbers, arrays, and objects, and returns At the client end, the data is encoded and transmitted according to different return formats. 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

ajaxReturn 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

The above is the detailed content of Is ajaxreturn a built-in method in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template