2017-02-24 94 views
-1

我正在尝试第一次在codeigniter中使用jquery ajax。我没有收到任何来自Ajax电话的回应。当我点击按钮时,我可以在j.ajax之前使用警报来验证数据,但对实际的ajax调用没有响应。请帮助找到问题。jquery ajax调用没有在codeigniter中提供响应

我的看法是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<script src="<?php echo base_url(); ?>js/jquery-latest.js" type="text/javascript"></script> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<script type="text/javascript"> 
var j=jQuery.noConflict(); 
j(document).ready(function(){ 
    j("#b").click(function(){ 
     var scode=j("#a").val(); 
     var baseurl="<?php echo base_url(); ?>"; 
     $.ajax({ 
     type: 'POST', 
     url: baseurl + 'ajax/aj', 
     data: {txt:scode}, 
     success:function(response){ 
      j("#c").val(response); 
     } 
    }); 
    }); 
}); 
</script> 
<form id="form1" name="form1" method="post" action=""> 
    <label for="a"></label> 
    <input type="text" name="a" id="a" /> 
    <input name="b" type="button" value="click" id="b" /> 
    <input type="text" name="c" id="c" /> 
</form> 
</body> 
</html> 

我的控制器:

<?php 
class ajax Extends CI_Controller{ 
    public function __construct() 
    { 
    parent::__construct(); 
    $this->load->helper('url'); 
    } 
    public function index(){ 
     $this->load->view('ajax_trial'); 
    } 
    public function aj(){ 
     $x=$this->input->get('txt'); 
     echo $x; 
    } 
} 
?> 
+0

你得到的控制器'$ x'什么价值? –

+0

我不使用模型 – Amit

+0

尝试在控制台中进行调试。 – DevOps

回答

0

只要改变POST的AJAX调用来获取为u使用GET方法来接收变量。我想你的代码如果你的点击功能正在工作,那么通过做这个小改变它将工作: -

jQuery("#b").click(function(){ 
      var scode=jQuery("#a").val(); 
      var baseurl="<?php echo base_url(); ?>"; 
      jQuery.ajax({ 
       type: 'GET', 
       url: baseurl + 'test/aj', 
       data: {txt:scode}, 
       success:function(response){ 
        console.log(response); 
        jQuery("#c").val(response); 
       } 
      }); 
     }); 
0

你有没有注意到你ar使用J的jQuery,但使用$ .ajax的Ajax调用?

+0

我试图通过更改j $ $ butconsole给出此错误
XMLHttpRequest无法加载http:// [:: 1]/ebizport/ajax/aj?txt = aa。请求的资源上没有“Access-Control-Allow-Origin”标题。因此不允许Origin'http:// localhost'访问。 – Amit

0

请使用此控制器

error_reporting(0);

header('content-type:application/json; charset = utf-8');

header('Access-Control-Allow-Origin:*'); (“Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept”);

2

亲爱的朋友,您使用POST方法发送数据,并使用你get方法,你必须使用POST方法打印数据....

<?php 
class ajax Extends CI_Controller{ 
    public function __construct() 
    { 
    parent::__construct(); 
    $this->load->helper('url'); 
    } 
    public function index(){ 
     $this->load->view('ajax_trial'); 
    } 
    public function aj(){ 
     $x=$this->input->post('txt'); 
     echo $x; 
    } 
} 
?>