【问题标题】:CodeIgniter - How to return Json response from controllerCodeIgniter - 如何从控制器返回 Json 响应
【发布时间】:2013-09-20 05:13:37
【问题描述】:

如何将控制器的响应返回给 Jquery Javascript?

Javascript

$('.signinform').submit(function() { 
   $(this).ajaxSubmit({ 
       type : "POST",
       url: 'index.php/user/signin', // target element(s) to be updated with server response 
       cache : false,
       success : onSuccessRegistered,
       error: onFailRegistered
   });        
   return false; 
}); 

数据返回 null(空白)!

function onSuccessRegistered(data){
    alert(data);
};

控制器 -

public function signin() {
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);    
    echo json_encode( $arr );
}

【问题讨论】:

  • 验证你是否调用了特定的方法..调用已到达登录方法
  • 是的,通话已到达登录
  • success : function(response){ console.log(response)} 修改这一行并添加header //add header here header('Content-Type: application/json')

标签: javascript php codeigniter


【解决方案1】:
return $this->output
            ->set_content_type('application/json')
            ->set_status_header(500)
            ->set_output(json_encode(array(
                    'text' => 'Error 500',
                    'type' => 'danger'
            )));

【讨论】:

  • 为什么“type”值是“danger”?
  • @VictorS 是你的响应数据,基本上是你想要的任何东西
  • 'type' => 'danger' 合作得很好,谢谢您的提示!
  • 拜托,对于 Codeigniter 4,请使用类似:return $this->response->setJSON( array( 'text' => 'Error 500', 'type' => 'danger' ) );
  • 这个答案缺少教育解释。
【解决方案2】:
//do the edit in your javascript

$('.signinform').submit(function() { 
   $(this).ajaxSubmit({ 
       type : "POST",
       //set the data type
       dataType:'json',
       url: 'index.php/user/signin', // target element(s) to be updated with server response 
       cache : false,
       //check this in Firefox browser
       success : function(response){ console.log(response); alert(response)},
       error: onFailRegistered
   });        
   return false; 
}); 


//controller function

public function signin() {
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);    

   //add the header here
    header('Content-Type: application/json');
    echo json_encode( $arr );
}

【讨论】:

  • 返回(空字符串)
  • 显示:{"a":1,"b":2,"c":3,"d":4,"e":5}
  • 那是正确的,那么您必须在该 ajax url 中添加完整的 URL:'http:///index.php/user/signin' 然后检查它
  • 成功了。但它覆盖了一些功能,例如输出类中的缓存。所以 Cliff Richard Anfone 的回答更好。
  • set_status_header 并不总是 500。我的响应返回状态码,因为 200 表示成功。 Cliff Richard Anfone 解释了如何返回内部服务器错误响应。
【解决方案3】:

对于CodeIgniter 4,可以使用内置的API Response Trait

下面是示例代码供参考:

<?php namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Home extends BaseController
{
    use ResponseTrait;

    public function index()
    {
        $data = [
            'data' => 'value1',
            'data2' => 'value2',
        ];

        return $this->respond($data);
    }
}

【讨论】:

    【解决方案4】:

    这不是你的答案,这是处理表单提交的另一种方式

    $('.signinform').click(function(e) { 
          e.preventDefault();
          $.ajax({
          type: "POST",
          url: 'index.php/user/signin', // target element(s) to be updated with server response 
          dataType:'json',
          success : function(response){ console.log(response); alert(response)}
         });
    }); 
    

    【讨论】:

      【解决方案5】:

      就我而言,我正在使用 ci4 ,我向 clinet 发送响应,如下所示: 例如在 App\Controllers\Category:setOrder 我的类别控制器扩展了 BaseController

        return $this->response->setJson(['msg'=>'update-success']);
      

      【讨论】:

        猜你喜欢
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多