在codeigniter(flashdata)中返回json对象

时间:2011-01-20 10:49:55

标签: jquery json codeigniter

如何在这种情况下将json对象(消息)返回到视图(admincp_index)。 下面的方法工作正常,但我真的想用一些动画来加强它

问候,菲尔

/* AdmincontrolPanel */
function index()
{
    $data['messages'] = $this->session->flashdata('messages');
    $data['content'] = 'admincp/admincp_index';
    $this->load->view('backend/template', $data);
}


 function applicant()
 {
      $id = $this->input->post('id');

      if($this->input->post('accept'))
      {
            if($this->admincpModel->accept_applicant($id) == TRUE)
            {
                 $this->session->set_flashdata('messages', '<div class="ok">Applicant Added!</div>');
                 redirect('admincp');
            }            
 }

/* admincp_index */

if($messages){
    // echo messages
}

3 个答案:

答案 0 :(得分:23)

使用code igniter的输出类来响应json。

$this->output
    ->set_content_type('application/json')
    ->set_output(json_encode(array('foo' => 'bar')));

答案 1 :(得分:13)

有三件事需要牢记:

  1. 浏览器可以缓存JSON响应,因此最好在URL的末尾附加时间戳以保持数据新鲜。 (对于GET方法也是如此,但不一定是POST)。

  2. JSON响应的内容类型必须为“ application / json ”或“ text / javascript ”。

  3. {5.2}中包含json_encode函数,因此较旧的环境可能无法使用它,您必须安装模块或编写自己的编码类。

  4. 我正在运行PHP 5.1.6的服务器上做一些工作,我不需要序列化任何复杂类型,所以我发现下面显示的技术工作正常。我正在使用一个简单的“JSON视图”,它在响应头中设置正确的内容类型,并发出一个JSON字符串,该字符串在控制器中手动连接。

    Phil,jQuery特效/动画可以利用 success 回调函数中返回的JSON数据。在下面的示例中,我只是在警告框中显示消息

    客户端代码:

    // the jQuery POST URL includes a time stamp
    var now = new Date();
    $.ajax({
        type: "POST",
        url: "page/post/" + now.valueOf().toString(),
        data: {},
        dataType: "json",
        success: function (result) {
            alert(result.message);
        }
    });
    

    Controller(/application/controllers/page.php):

    class Page extends CI_Controller {
    
        function __construct()
        {
            parent::__construct();
        }
    
        function index()
        {
        }
    
        function post($TimeStamp)
        {
            /* process request... $Timestamp may or may not be used here. */
    
            $data['json'] = '{"message":"The post was handled successfully."}';
            $this->load->view('json_view', $data);
        }
    }
    

    查看(/application/views/json_view.php):

    <?php
    $this->output->set_header('Content-Type: application/json; charset=utf-8');
    echo $json;
    ?>
    

答案 2 :(得分:0)

你做错了路。如果你想获得json对象,AJAX是处理这个问题的最好方法。 在您的admincp索引视图中(使用jquery)

$.ajax({
        type: 'POST',
        url: 'controller/applicant',
        data: 'your post data',
        success: function(response) {
            var response = $.evalJSON(r);
            if(response.message) {
               //do some animation
            }
        }
    });

申请人方法

function applicant()
{
  $id = $this->input->post('id');
  if($this->input->post('accept'))
  {
        if($this->admincpModel->accept_applicant($id) == TRUE)
        {
             echo json_encode(array('message'=>'<div class="ok">Applicant Added!</div>'));
             exit();
        }         
   }
相关问题