I learned about thinkphp framework Ajax (I'm sorry for all the respondents because I was too lazy and didn't put code in the previous step). my idea is to trigger Ajax at the front end -- & amp; gt; go to the controller -- & amp; gt; fetch data from the database -- & amp; gt; how can I directly return to the front-end to process data without passing the template's response...
Learn about thinkphp framework Ajax (sorry to all the respondents because I was too lazy and didn't put the code in it)
My idea is: trigger Ajax at the front end --> go to the controller --> fetch data from the database --> directly return to the front end to process data.
How can I directly return data from the controller without rendering the template?
The problem should be caused by $ this-> ajaxReturn ();
I deleted ajaxReturn () and switched to var_dump ($ data)
The front-end receives the following data:
The problem is finally solved because it is not found to be in json format, and ajaxReturn () is not transmitted in json format, therefore, the front-end receives array ([0] => array ({}), and therefore $. parseJSON (data) parsing,
Use $. each (data [0], function (I, n) {}) to parse data.
Reply content:
Learn about thinkphp framework Ajax (sorry to all the respondents because I was too lazy and didn't put the code in it)
My idea is: trigger Ajax at the front end --> go to the controller --> fetch data from the database --> directly return to the front end to process data.
How can I directly return data from the controller without rendering the template?
The problem should be caused by $ this-> ajaxReturn ();
I deleted ajaxReturn () and switched to var_dump ($ data)
The front-end receives the following data:
The problem is finally solved because it is not found to be in json format, and ajaxReturn () is not transmitted in json format, therefore, the front-end receives array ([0] => array ({}), and therefore $. parseJSON (data) parsing,
Use $. each (data [0], function (I, n) {}) to parse data.
I don't quite understand what you mean. Ajax requests are rendered to the front-end by the front-end template engine after obtaining data from the background. how can I "directly return data from the controller through template rendering "?, Do I still need a template engine to return data? The use of the template engine is to avoid concatenating strings for better processing of ajax returned data. Because ajax request data is often used in projects, you can encapsulate the method yourself, for example, my method:
Protected $ msg; // protected function fail ($ mode = false, $ type = "json ") {$ this-> msg ['status'] = 'failed'; if (true = $ mode) {$ this-> reply ($ type );}} // the returned data is successfully protected function succeed ($ mode = false, $ type = "json") {$ this-> msg ['status'] = 'success '; if (true ===$ mode) {$ this-> reply ($ type) ;}// the returned data 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 ;}
Usage:
1. Successful request:
$ This-> setResult ($ data );
$ This-> succeed (true)
2. request failed:
$ This-> setResult ("request failed") // You do not need to write
$ This-> fail (true)
Ajax requests in js:
$. Post ('', $ data ). done (function (rs) {var rs = $. parseJSON (rs); if (rs. status = 'success') {var msg = rs. result; // Get Data} else {// process failed requests }});
$ This-> ajaxReturn ($ data );
Previously
$ This-> assian ('data', $ data );
$ This-> display ();
Now yes
$ This-> ajaxReturn ($ data );
Nice! Learning!