Thinkphp Ajax implementation principle

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.