一个div里面Ajax响应 [英] Ajax response inside a div

查看:115
本文介绍了一个div里面Ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以显示其中一个div和我有以下的code。在我的视图文件。

I am trying to show the value of ajax response inside a div and for that I have the following code in my view file.

<script type="text/javascript" src="MY LINK TO JQUERY"></script>

<script  type="text/javascript">
     $(function(){ // added
     $('a.vote').click(function(){
         var a_href = $(this).attr('href');

     $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>contents/hello",
            data: "id="+a_href,
            success: function(server_response){
                             if(server_response == 'success'){
                                  $("#result").html(server_response); 
                             } 
                             else{
                                  alert('Not OKay');
                                 }

                      }
  });   //$.ajax ends here

  return false
    });//.click function ends here
  }); // function ends here
 </script>

  <a href="1" title="vote" class="vote" >Up Vote</a>
  <br>
  <div class="result"></div>                                        

我控制器(到了AJAX发送的值):

my Controller (to which the ajax is sending the value):

function hello() {
              $id=$this->input->post('id');
              echo $id;
             }      

现在我试图做到的,是让server_response值侧(即正在从控制器发送的值)&LT; D​​IV CLASS =结果&GT;&LT; / DIV&GT; 在我的视图文件。

Now what I am trying achieve is get the server_response value (the value that is being sent from the controller) in side <div class="result"></div> in my view file.

我曾尝试以下code,但它没有显示在div内的值。

I have tried the following code but its not showing the value inside the div.

您能告诉我问题出在哪里?

Could you please tell me where the problem is?

推荐答案

现在的问题是,你已经混阿贾克斯<$参数C $ C>成功处理。首先进入数据,你的脚本给背部,然后进入 textStatus 。理论上它可以超时,错误,notmodified,成功或parsererror。然而,在成功 textStatus永远是成功的。但是,如果你需要添加警告上的错误,你可以添加错误处理程序。是的,改变选择器$(#结果)上课。因此,纠正code可能是这样的:

The problem is that you have mixed arguments of Ajax success handler. First goes data which your script gives back, then goes textStatus. Theoretically it can be "timeout", "error", "notmodified", "success" or "parsererror". However, in success textStatus will always be successful. But if you need to add alert on error you can add error handler. And yes, change selector in $("#result") to class. So corrected code may look like this:

$.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>contents/hello",
    data: "id=" + a_href,
    success: function(data, textStatus) {
        $(".result").html(data);    
    },
    error: function() {
        alert('Not OKay');
    }
});​

这篇关于一个div里面Ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆