Home >Backend Development >PHP Tutorial >thinkphp's Ajax implementation principle

thinkphp's Ajax implementation principle

WBOY
WBOYOriginal
2016-08-04 09:21:241146browse

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->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;
  }</code>

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>

$this->ajaxReturn($data);

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

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

Not bad! learned!

Statement:
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