【问题标题】:Returning a json object in codeigniter(flashdata)在 codeigniter(flashdata) 中返回一个 json 对象
【发布时间】:2011-01-20 10:49:55
【问题描述】:

在这种情况下(消息)如何将 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
}

【问题讨论】:

  • 您能解释一下动画与在这种情况下使用 JSON 有什么关系吗?
  • 他们没有,我只是想将它作为 JSON 对象返回,然后对其应用一些 jquery 动画
  • $(function(){ $('.ok').fadeIn('slow'); }): ?
  • 您可以使用json_encode 创建一个 JSON 字符串。 php.net/json_encode

标签: jquery json codeigniter


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    需要牢记三点:

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

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

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

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

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

    客户端代码:

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

    控制器(/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;
    ?>
    

    【讨论】:

    • output->set_header('Content-Type: application/json; charset=utf-8');回声 json_encode($json); ?>
    【解决方案3】:

    你做错了。如果你想获取 json 对象,AJAX 是最好的处理方式。 在您的 admincp_index 视图中(使用 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();
            }         
       }
    

    【讨论】:

    • 不要使用$.evalJSON。相反,将dataType: 'json' 添加到$.ajax 调用中。传递给成功函数的响应变量将自动解析为 JSON。此外,在 jQuery 中,它是 $.parseJSON,而不是 $.evalJSON
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 2013-12-29
    相关资源
    最近更新 更多