This article mainly introduces the new features of ThinkPHP3.1 to support Ajax more comprehensively. It has certain reference value. Now I share it with you. Friends in need can refer to this article
The article mainly introduces ThinkPHP3.1’s support for Ajax. Friends who need it can refer to
ThinkPHP3.1 version’s support for AJAX is more complete, as shown in:
1 .Judge the AJAX method improvement
Now you can directly use the constant IS_AJAX to determine whether the AJAX request is made, replacing the previous isAjax method of the Action class. The advantage is that it can be judged in any code. The error and success methods of the Action class have built-in support for automatic AJAX judgment.
2. The 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 returns Data information can only be passed through the ajaxAssign method, and ThinkPHP3.1 version has improved the ajaxReturn method itself and can better support ajax data expansion, for example:
$data['status'] = 1; $data['info'] = '返回信息'; $data['data'] = '返回数据'; $data['url'] = 'URL地址'; $this->ajaxReturn($data);
data value array can be defined arbitrarily.
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 info and 1 parameters into the $data array , which is equivalent to assigning
$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 have improved support for ajax.
If it is the ajax mode, the success and error methods of the Action class have been improved and supported. The parameters of the method 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 the 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 ultimately returned to the client is an array, including name, code, info, status and url.
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to thinkphp’s implementation of the password retrieval function for sending emails
Use pthreads to implement real PHP Multi-threading method
The above is the detailed content of A more complete introduction to the new features of ThinkPHP3.1 that support Ajax. For more information, please follow other related articles on the PHP Chinese website!