【问题标题】:Json Encoding IssueJson 编码问题
【发布时间】:2012-02-16 00:15:57
【问题描述】:

由于某种原因,当我提交表单时,它显示消息:json_encode() 期望参数 2 很长,在控制器的这一行给出了字符串,不知道为什么。有谁知道是什么问题?

控制器

function forgot_password_submit() 
{
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');

    if (!$this->form_validation->run()) 
    {
        $this->kow_auth->output('There was a problem submitting the form! Please refresh the window and try again!', FALSE);
        return;
    }

    $user_data = $this->users->get_user_by_username($this->input->post('username'));
    if ($user_data === NULL) 
    {
        $this->kow_auth->output('User does not exist in the database!', FALSE);
        return;
    }

    $already_sent_password = (isset($user_data->new_password_key) && isset($user_data->new_password_requested));
    if ($already_sent_password) 
    {
        $this->kow_auth->output('Check your email for your temporary password!');
        return;
    }

    if (!strtotime($user_data->new_password_requested) <= (time() - 172800)) 
    {
        $this->kow_auth->output('You have to wait 2 days before a new temp password can be emailed!', FALSE);
    } 
    else 
    {
        if ($this->kow_auth->forgot_password($this->input->post('username'))) 
        {
            $this->kow_auth->send_email('forgot_password', 'KOW Manager Forgot Password Email', $user_data);
            $this->kow_auth->output('A temporary password has been emailed to you!');
        } 
        else 
        {
            $this->kow_auth->output('A temporary password could not be created for you!', FALSE);
        }
    }
}

kow_auth 库

/**
* Generate output message
*
* @param string
* @return object
*/
function output($message, $success = TRUE) 
{
    $status = $success ? array('succes' => 'yes') : array('error' => 'yes');
    echo json_encode($status, $message);
}

【问题讨论】:

  • 你真的应该在视图中回显 json,这违背了 MVC 模式。

标签: php json codeigniter encode


【解决方案1】:

是的,json_encode 的第二个参数应该是选项的位掩码,而不是消息。您一次只能编码一件事,而不是字符串 数组。也许你想要json_encode(array($status, $message)) 之类的东西?

【讨论】:

  • 它应该在哪里取代消息。
  • 这太模糊了。您不能使用 json_encode 句号以这种方式对消息和状态进行编码。你期望输出是什么?无论消息输出发生什么,都是一个完全不同的话题。
猜你喜欢
  • 2012-12-12
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多