thinkphp's Ajax implementation principle

WBOY
Release: 2016-08-04 09:21:24
Original
1064 people have browsed it

Learn thinkphp framework Ajax (since I was too lazy to put the code just now, I apologize to all the responders)
My idea is: trigger Ajax in the front desk -->> Enter the controller -->> Fetch from the database Number-->> Directly return data to the foreground processing
How can I return data directly from the controller without rendering the template?
thinkphp's Ajax implementation principle

thinkphp's Ajax implementation principle

thinkphp's Ajax implementation principle

The problem should be $this->ajaxReturn();
I deleted ajaxReturn() and used var_dump($data) instead
The data received by the front desk is as follows
thinkphp's Ajax implementation principle

The problem is finally solved. The reason is that what is found is not in json format. What ajaxReturn() transmits is not in json format, so what the front desk receives is array([0]=>array({})), so it cannot Use $.parseJSON(data) to parse,
use $.each(data[0], function(i,n){}) instead to parse data
thinkphp's Ajax implementation principle

Reply content:

Learn thinkphp framework Ajax (since I was too lazy to put the code just now, I apologize to all the responders)
My idea is: trigger Ajax in the front desk -->> Enter the controller -->> Fetch from the database Number-->> Directly return data to the foreground processing
How can I return data directly from the controller without rendering the template?
thinkphp's Ajax implementation principle

thinkphp's Ajax implementation principle

thinkphp's Ajax implementation principle

The problem should be $this->ajaxReturn();
I deleted ajaxReturn() and used var_dump($data) instead
The data received by the front desk is as follows
thinkphp's Ajax implementation principle

The problem is finally solved. The reason is that what is found is not in json format. What ajaxReturn() transmits is not in json format, so what the front desk receives is array([0]=>array({})), so it cannot Use $.parseJSON(data) to parse,
use $.each(data[0], function(i,n){}) instead to parse data
thinkphp's Ajax implementation principle

I don’t quite understand what you mean. Ajax requests to the background to obtain data and then renders it to the front-end through the front-end template engine. How do you mean “returning data directly from the controller through template rendering”? , is a template engine still needed to return data? The use of the template engine is to avoid splicing strings and facilitate better processing of the data returned by ajax. Because ajax request data is often used in projects, you can encapsulate the method yourself, such as the way I use:

protected $msg; //返回数据失败 protected function fail($mode=false,$type="json"){ $this->msg['status'] = 'failure'; if(true===$mode){ $this->reply($type); } } //返回数据成功 protected function succeed($mode=false,$type="json"){ $this->msg['status'] = 'success'; if(true===$mode){ $this->reply($type); } } //返回的数据 protected function setResult($result){ $this->msg['result'] = $result; } protected function reply($type="json"){ if($type == 'string'){ print_r($this->msg); }else{ echo json_encode($this->msg); } exit; }
Copy after login

Usage:
1. Request successful:
$this->setResult($data);
$this->succeed(true)
2. Request failed:
$this->setResult("Request failed" ) //No need to write
$this->fail(true)

ajax request in js:

$.post('',$data).done(function(rs){ var rs = $.parseJSON(rs); if(rs.status == 'success') { var msg = rs.result; //获取数据 } else { //请求失败的处理 } });
Copy after login

$this->ajaxReturn($data);

It used to be
$this->assian('data', $data);
$this->display();

Now is
$this->ajaxReturn($data);

Not bad! learned!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!