Table of Contents
Reply content:
Home Backend Development PHP Tutorial thinkphp's Ajax implementation principle

thinkphp's Ajax implementation principle

Aug 04, 2016 am 09:21 AM
php thinkphp

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:

<code>protected $msg;

 //返回数据失败
 protected function fail($mode=false,$type="json"){
    $this-&gt;msg['status'] = 'failure';
    if(true===$mode){
      $this-&gt;reply($type);
    }
  }
//返回数据成功
protected function succeed($mode=false,$type="json"){
    $this-&gt;msg['status'] = 'success';
    if(true===$mode){
      $this-&gt;reply($type);
    }
  }
//返回的数据
protected function setResult($result){
    $this-&gt;msg['result'] = $result;
  }
protected function reply($type="json"){
    if($type == 'string'){
      print_r($this-&gt;msg);
    }else{
      echo json_encode($this-&gt;msg);
    }
    exit;
  }</code>
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:

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

$this->ajaxReturn($data);

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

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

Not bad! learned!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles