【问题标题】:Codeigniter - How to access user defined function using ajax?Codeigniter - 如何使用 ajax 访问用户定义的函数?
【发布时间】:2016-06-16 13:23:28
【问题描述】:

我正在尝试使用 ajax 调用用户定义的库函数,如下所示。

$(document).ready(function() 
{
        $("a.refresh").click(function() 
        {
            jQuery.ajax(
            {
                type: "POST",
                url: "<?php echo base_url(); ?>" + "application/libraries/Captcha_lib/captcha_refresh",
                success: function(res) 
                {
                    if (res)
                    {
                        jQuery("span.image").html(res);
                    }
                }
            });
        });
});

我在 firebug 中得到以下响应:

您无权访问请求的对象。 服务器要么读保护要么不可读。

注意: 如果我将 captcha_refresh 函数 放在我的登录控制器中并将 URL 传递给 ajax,如下所示

jQuery.ajax( { 类型:“发布”, url: "" + "登录/验证码刷新", ..... });

然后它工作正常。但是我不想这样做。

这是我的 captcha_refresh 函数

public function captcha_refresh()
{
        $values = array(
            'word' => '',
            'word_length' => 8,
            'img_path' => './hrms_assets/captcha_img/',
            'img_url' => base_url() .'hrms_assets/captcha_img/',
            'font_path' => base_url() . 'system/fonts/texb.ttf',
            'img_width' => '150',
            'img_height' => 50,
            'expiration' => 3600
        );
        $data = create_captcha($values);
        $this->session->userdata['captchaWord'] = $data['word'];
        echo $data['image'];
}

【问题讨论】:

  • 您正在尝试直接访问库,我认为您必须从您的 ajax 调用控制器,并且在该控制器中您必须调用库函数。
  • 是的,我的错误是直接调用库函数。谢谢你的答复。 @kishor10d
  • 那么,它现在工作了吗??
  • 是的,兄弟。我在提出问题的同一天解决了这个问题。
  • 上传您的答案或为对您有帮助的答案投票

标签: jquery ajax codeigniter codeigniter-3


【解决方案1】:

1) 在控制器中创建方法 2) 在控制器中调用相同的库函数 captch_refresh 并从库函数返回数据。 3) 从控制器的方法发送响应

Ajax 调用网址:

// Controller Code
class Login extends MY_Controller {

public function captcha_refresh() {
   // load library
   $this->load->library('captcha_lib');
   // call library function
   echo $this->captcha_lib->captcha_refresh();
   exit(); 
}


// Make Library method
class Captcha_lib {
public function captcha_refresh(){
    $CI =& get_instance();

    $values = array(
    'word' => '',
    'word_length' => 8,
    'img_path' => './hrms_assets/captcha_img/',
    'img_url' => base_url() .'hrms_assets/captcha_img/',
    'font_path' => base_url() . 'system/fonts/texb.ttf',
    'img_width' => '150',
    'img_height' => 50,
    'expiration' => 3600
    );
    $data = create_captcha($values);
    $CI->session->userdata['captchaWord'] = $data['word'];
    return $data['image'];
}

}

// jQuery 代码

$(document).ready(function() {
    $("a.refresh").click(function() {
    jQuery.ajax({
    type: "POST",
    url: "<?php echo base_url('login/captcha_refresh'); ?>",
    success: function(res) {
    if (res)
    {
    jQuery("span.image").html(res);
    }
    }
    });
    });
    });

【讨论】:

  • 你能分享一些代码吗?我不明白。
【解决方案2】:

试试这个:

$(document).ready(function() {
            $("a.refresh").click(function() {
               jQuery.ajax({
                 type: "POST",
                 url: "<?php echo APPPATH; ?>" + "libraries/Captcha_lib/captcha_refresh",
                 success: function(res) {
                    if (res)
                    {
                      jQuery("span.image").html(res);
                    }
                 }
               });
             });
            });

或者创建一个控制器并调用你那里的库来执行你的功能。

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->libraray('captcha_lib');
        echo $this->captcha_lib->captcha_refresh();
    }
}

在这之后你的 ajax url 应该是这样的:url: "&lt;?php echo base_url('welcome/index'); ?&gt;",

【讨论】:

    【解决方案3】:

    你做错了,你的代码:-

        jQuery.ajax({
    
            type: "POST",
            url: "<?php echo base_url(); ?>" + "application/libraries/Captcha_lib/captcha_refresh",
            success: function(res) {
    
                 .....
    

    在这种情况下,您的 URL 将是这样的 "http://your-site/application/libraries/Captcha_lib/captcha_refresh" 。这意味着您将在此 URL 上发出请求,并且只有在以下情况下才会回复您:-

    • 您使用 URL Rewrite 将其转换为实际 URL
    • 这是原始网址

    但在你的情况下,没有一个是真的。您的实际 URL 是 "http://my-site/application/libraries/Captcha_lib.php" 并且 captcha_refresh 是文件 Captcha_lib.php 中的一个函数。所以它不会只通过调用 Captcha_lib.php 文件来执行。就像您有一个基于类但没有 ma​​in() 函数的文件。

    所以最好使用 MVC(模型视图控制器)技术。您可以像这样创建一个控制器并在控制器中加载库:-

     <?php 
    
     # application/controllers/HandleMe.php
    
     class HandleMe extends CI_Controller
     {
    
          function index()
          {
                $this->load->library('Captcha_lib');
                echo $this->captcha_lib->captcha_refresh();
          }
     }
    

    你的javascript代码应该是

       jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "index.php/HandleMe",
        .....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2017-09-13
      • 2013-05-07
      • 1970-01-01
      相关资源
      最近更新 更多