【问题标题】:CodeIgniter AJAX Call to undefined function ajax_response()CodeIgniter AJAX 调用未定义的函数 ajax_response()
【发布时间】:2021-09-17 15:32:40
【问题描述】:

我在 ajax_response 中有问题 我的控制器中有这样的脚本

class Ajax extends CI_Controller
{

    public function get_data($op_produk = '')
    {
        if ($op_produk=='' || $op_id=='')
        {
        ajax_response('ID Voucher tidak benar.', 'failed');
        }
       
    }
}

在我像 domain.com/get_data 这样的 URL 中执行之后

它应该显示这样的消息

{"status":false,"message":"ID Voucher tidak benar.","alert":"failed"}

但我得到了这个错误

A PHP Error was encountered
Severity: Error

Message: Call to undefined function ajax_response()

Filename: controllers/Ajax.php

Line Number: 23

Backtrace:

这是什么问题?我尝试在 Cpanel 中使用,但在 webuzo 中我收到了错误消息?

【问题讨论】:

  • 函数 ajax_response() 在哪里?您只是在寻找一种输出响应的方法吗?
  • ajax_response 定义在哪里?
  • @Kinglish 是的.. 我想显示输出响应
  • @DevsiOdedra - 无法定义,错误是这样说¯\_(ツ)_/¯

标签: javascript php jquery ajax codeigniter


【解决方案1】:

您需要为此明确设置输出 - 这意味着您创建一个数组,然后通过 json_encode 回显它。要在 codeigniter 中输出这样的 ajax 响应,最好使用 Output 类。

public function get_data($op_produk = '') {
  if ($op_produk == '' || $op_id == '') {
  $this->output
     ->set_content_type('application/json')
     ->set_output(json_encode(array(
       'message' => 'ID Voucher tidak benar.', 
       'alert' => 'failed', 
       'status' => 'false')));
       exit;
  }
}

【讨论】:

    猜你喜欢
    • 2016-04-06
    • 2012-02-16
    • 2018-05-05
    • 2017-03-01
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 2016-08-17
    • 2021-11-27
    相关资源
    最近更新 更多