Home >Backend Development >PHP Tutorial >New features of ThinkPHP3.1 include more complete support for Ajax_PHP Tutorial
ThinkPHP version 3.1 has more complete support for AJAX, as shown in:
1. Determine the improvement of AJAX methods
Now you can directly use the constant IS_AJAX to determine whether the request is in AJAX mode, replacing the previous isAjax method of the Action class. The advantage is that it can be determined in any code. The error and success methods of the Action class have built-in support for automatic AJAX judgment.
2.ajaxReturn method is improved
The original ajaxReturn method can only return fixed-structure data, including data, status and info index information. If you need to expand additional return data information, you can only use the ajaxAssign method. The ThinkPHP 3.1 version has improved the ajaxReturn method itself. , can better support ajax data extension, for example:
$data['status'] = 1; $data['info'] = '返回信息'; $data['data'] = '返回数据'; $data['url'] = 'URL地址'; $this->ajaxReturn($data);
The data value array can be defined at will.
The improved ajaxReturn method is also compatible with the previous writing method, for example:
$this->ajaxReturn($data,'info',1);
The system will automatically merge the two parameters info and 1 into the $data array, which is equivalent to assignment
$data['info'] = 'info'; $data['status'] = 1; $data['data'] = $data; $this->ajaxReturn($data);
But this usage is no longer recommended.
3. The success and error methods improve ajax support
If it is in ajax mode, the success and error methods of the Action class have been improved and supported. The parameters of these two methods will be converted into the info, status and url parameters of the data data of the ajaxReturn method. It can also support passing in other parameters. There are two ways to support ajax value passing. Taking the success method as an example, the first way is to directly pass in ajax data
$data['code'] = 200; $data['name'] = 'ThinkPHP'; $this->success('成功提示信息','跳转地址',$data);
or use
$this->assign('code',200); $this->assign('name','thinkphp'); $this->success('成功提示信息','跳转地址');
The ajax data information finally returned to the client is an array, including name, code, info, status and url.