Reputation: 2276
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'];
}
Upvotes: 1
Views: 1023
Reputation: 1600
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 :-
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",
.....
Upvotes: 0
Reputation: 721
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'); ?>",
Upvotes: 0
Reputation: 64
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);
}
}
});
});
});
Upvotes: 2