简体   繁体   English

jQuery Ajax不适用于Codeigniter

[英]Jquery Ajax is not working with Codeigniter

I am a ajax beginner, Here I am trying to show a text box value in same page using Ajax. 我是ajax初学者,在这里我试图使用Ajax在同一页面中显示文本框值。

My Controller code: 我的控制器代码:

<?php
class Merchant extends CI_Controller
 {

    public function ajaxtest()
    { 

     $this->load->helper('url');
     $this->load->view('ajaxtest');
     $fullname = $this->input->post("fullname");
     echo $fullname;

    }
}
?>

Here is my view code: 这是我的查看代码:

<head>
 <script src="<?php echo base_url();?>assets/js/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){   

    $("#getinfo").click(function()
    {   

     $.ajax({
         type: "POST",
         url: "<?php echo base_url(); ?>merchant/ajaxtest",
         data: {textbox: $("#fullname").val()},
         dataType: "text",  
         cache:false,
         success: 
              function(data){                    
                $('#mytext').html(data);
              }
          });
     return false;
 });
 });

</script>
 </head>
 <body>
 <form method="post">
        <input  type="text" id="fullname"/>
        <input  type="button" value="getinfo" id="getinfo"/>      
      <span id="mytext"></span>
 </form>

 </body>

When I click on the button getinfo, I want to show the text inside the text box as span text. 当我单击按钮getinfo时,我想将文本框中的文本显示为跨度文本。 But now it shows nothing.. 但是现在它什么也没显示。

Updated: 更新:

After experts' opinion, I edited some text(see my edit note), Now When i click on the button, it shows again a textbox and a button.. !! 经过专家的意见,我编辑了一些文本(请参阅编辑说明)。现在,当我单击按钮时,它再次显示一个文本框和一个按钮。

在此处输入图片说明

Did you set the base_url variable with a link on the Javascript? 您是否使用Javascript上的链接设置了base_url变量? Because your post url contains this variable and you need set this to make it work. 由于您的帖子网址包含此变量,因此需要对其进行设置以使其起作用。 So initialize the variable with the base_url link. 因此,使用base_url链接初始化变量。

See the corrected example below . 请参阅下面的更正示例。 Set your domain instead of the yourbaseurl.com 设置您的域,而不是yourbaseurl.com

<script type="text/javascript">
$(document).ready(function(){   
    var base_url='http://yourbaseurl.com/index.php/';
    $("#getinfo").click(function()
    {   

     $.ajax({
         type: "POST",
         url: base_url + "merchant/ajaxtest", 
         data: {textbox: $("#fullname").val()},
         dataType: "text",  
         cache:false,
         success: 
              function(data){                    
                $('#mytext').html(data);
              }
          });
     return false;
 });
 });

</script>

you are passing in textbox as parameter from your ajax to controller and trying to get POST data with name fullname. 您正在将文本框作为参数从ajax传递到控制器,并尝试获取名称为fullname的POST数据。 That wont work, since you passed in the name of parameter as textbox, access that in your post, as : 这将不起作用,因为您将参数名称作为文本框传入,因此可以在帖子中以如下方式访问该参数:

class Merchant extends CI_Controller
 {

    public function ajaxtest()
    { 
     $this->load->helper('url');
     //you dont need to load view so comment it
     //$this->load->view('ajaxtest');
     $fullname = $this->input->post("textbox"); //not fullname
     echo $fullname;
    }
}

js JS

<script type="text/javascript">
$(document).ready(function(){   
    var base_url='http://yourbaseurl.com/index.php/';
    $("#getinfo").click(function() {

    var fullname = $("#fullname").val();
    alert("Fullname:" + fullname); //do you get this alert   

     $.ajax({
         type: "POST",
         url: base_url + "merchant/ajaxtest", 
         data: {textbox: fullname},
         cache:false,
         success:function(data){
           alert("Response:" + data); //do you get this alert                 
           $('#mytext').html(data);
         }
     });
     return false;
 });
 });

</script>

Your base_url variable seems to be undefined in your JavaScript. 您的base_url变量似乎在JavaScript中未定义。

One simple approach to get the base URL is to echo it out in a hidden input, and then grab the value of that input in your JS code: 获取基本URL的一种简单方法是在隐藏的输入中将其回显,然后在JS代码中获取该输入的值:

HTML HTML

<input type='hidden' id="baseUrl" value="<?php  echo base_url(); ?>" /> 

JS JS

var base_url = $('#baseUrl').val(); 


$.ajax({
     type: "POST",
     url: base_url + "/merchant/ajaxtest", 
     data: {textbox: $("#fullname").val()},
     dataType: "text",  
     // ... 

Try using this: 尝试使用此:

<base href="<?=base_url();?>">
<script src="assets/js/jquery-latest.min.js"></script>

And this in ajaxtest: 而在ajaxtest中:

$this->load->helper('url');

And also Comment out this: 并注释掉:

// $this->load->view('ajaxtest');

Might be a little late with this response - but someone might find this while searching for a solution. 此响应可能会有点晚-但是有人在寻找解决方案时可能会发现此问题。 I was having the same issues with Codeigniter and JQuery ajax/post response. 我在Codeigniter和JQuery ajax / post响应中遇到了相同的问题。 I could not get this to work no matter what I tried. 无论我如何尝试,都无法使它正常工作。 In the end, it turned out to be php_error that was causing the problem. 最后,原来是导致问题的php_error。 Once I removed it, everything worked fine with my post/response. 删除它后,我的帖子/回复一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM