Jquery Codeigniter-如何使用ajax访问用户定义的函数?
我尝试使用ajax调用用户定义的库函数,如下所示Jquery Codeigniter-如何使用ajax访问用户定义的函数?,jquery,ajax,codeigniter,codeigniter-3,Jquery,Ajax,Codeigniter,Codeigniter 3,我尝试使用ajax调用用户定义的库函数,如下所示 $(document).ready(function() { $("a.refresh").click(function() { jQuery.ajax( { type: "POST", url: "<?php echo base_url(); ?>" + "application/lib
$(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);
}
}
});
});
});
1) 在控制器中创建方法
2) 在控制器中调用相同的库函数captch_refresh,并从库函数返回数据。
3) 从控制器的方法发送响应
Ajax调用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代码
$(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);
}
}
});
});
});
$(文档).ready(函数(){
$(“a.refresh”)。单击(函数(){
jQuery.ajax({
类型:“POST”,
url:“”,
成功:功能(res){
如果(res)
{
jQuery(“span.image”).html(res);
}
}
});
});
});
试试这个:
$(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);
}
}
});
});
});
在此之后,您的ajax url应该如下所示:
url:,
您的代码:-
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "application/libraries/Captcha_lib/captcha_refresh",
success: function(res) {
.....
jQuery.ajax({
类型:“POST”,
url:“+”应用程序/库/验证码库/验证码刷新”,
成功:功能(res){
.....
在此情况下,您的URL将与此类似。”。这意味着您将在此URL上发出请求,并且只有在以下情况下,它才会对您作出响应:-
- 您可以使用URL重写将其转换为实际URL
- 这是原始URL
<?php
# application/controllers/HandleMe.php
class HandleMe extends CI_Controller
{
function index()
{
$this->load->library('Captcha_lib');
echo $this->captcha_lib->captcha_refresh();
}
}
你能分享一点代码吗?我不明白。你试图直接访问库,我想你必须从ajax调用控制器,在该控制器中你必须调用库函数。是的,我的错误是直接调用库函数。谢谢你的回复。@kishor10dSo,它现在工作了吗?是的,兄弟。我解决了这个问题就在我问这个问题的同一天,上传你的答案或者上传对你有帮助的答案
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "application/libraries/Captcha_lib/captcha_refresh",
success: function(res) {
.....
<?php
# application/controllers/HandleMe.php
class HandleMe extends CI_Controller
{
function index()
{
$this->load->library('Captcha_lib');
echo $this->captcha_lib->captcha_refresh();
}
}
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/HandleMe",
.....