1

I am trying to call the user defined library function using ajax as below.

$(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);
                    }
                }
            });
        });
});

I'm getting the following response in firebug:

You don't have permission to access the requested object. It is either read-protected or not readable by the server.

Note: If i put the captcha_refresh function in my Login controller and pass the URL to ajax like below

jQuery.ajax( { type: "POST", url: "" + "Login/captcha_refresh", ..... });

then it is working fine. However i don't want to do it.

Here is my captcha_refresh function:

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'];
}
6
  • You are trying to access library directly, I think you have to call controller from your ajax and in that controller you have to call the library function. Commented Nov 9, 2016 at 10:05
  • Yes, my mistake was to call library function directly. Thanks for reply. @kishor10d Commented Nov 10, 2016 at 9:43
  • So, is it working now?? Commented Nov 10, 2016 at 9:46
  • yes bro. I resolved this issue on the same day i had asked the question. Commented Nov 10, 2016 at 9:54
  • Upload your answer or upvote the answer which helps you Commented Nov 10, 2016 at 9:56

3 Answers 3

2

1) Create method in controller 2) Call Same library function captch_refresh in controller and return data from library function. 3) Send response from controller's method

Ajax Call Url:

// 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 Code

$(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);
    }
    }
    });
    });
    });
Sign up to request clarification or add additional context in comments.

1 Comment

can you please share a bit of code. I don't understand.
0

Try this one:

$(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);
                    }
                 }
               });
             });
            });

Or Create a controller & call you library over there to execute you function.

class Welcome extends CI_Controller {

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

After this your ajax url should be like this: url: "<?php echo base_url('welcome/index'); ?>",

Comments

0

You are doing in wrong way , your code :-

    jQuery.ajax({

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

             .....

In this your URL will be like this "http://your-site/application/libraries/Captcha_lib/captcha_refresh" . it means you will make a request on this URL and it will give response to you only if :-

  • You uses URL Rewrite to convert this into an actual URL
  • This is original URL

But in your case none is true . Your actual URL is "http://my-site/application/libraries/Captcha_lib.php" and captcha_refresh is a function inside the file Captcha_lib.php . So it is not going to execute by calling Captcha_lib.php file only . Its like that you have a file with class based but there are no main() function .

So better to use MVC (Model View Controller) technique. You can create a controller and load the library in the controller like this :-

 <?php 

 # application/controllers/HandleMe.php

 class HandleMe extends CI_Controller
 {

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

And your javascript code should be

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.