jQuery div 内的 Ajax 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10575928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:33:33  来源:igfitidea点击:

Ajax response inside a div

ajaxjquery

提问by black_belt

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.

我试图在 div 中显示 ajax 响应的值,为此我在我的视图文件中有以下代码。

<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>                                        

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

我的控制器(ajax 向其发送值):

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

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.

现在我想要实现的是<div class="result"></div>在我的视图文件中获取 server_response 值(从控制器发送的值)。

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

我尝试了以下代码,但它没有显示 div 中的值。

Could you please tell me where the problem is?

你能告诉我问题出在哪里吗?

采纳答案by VisioN

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

问题是你有Ajax successhandler 的混合参数。首先是data你的脚本返回的,然后是textStatus. 理论上它可以是“超时”、“错误”、“未修改”、“成功”或“解析器错误”。但是,在successtextStatus 中总是会成功的。但是如果您需要添加alert错误,您可以添加error处理程序。是的,将 $("#result") 中的选择器更改为类。因此更正后的代码可能如下所示:

$.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');
    }
});?

回答by gdoron is supporting Monica

success: function(server_response) {
        $(".result").html(server_response);
}?

<div class="result"></div> // result is the class

The selector should be .resultnot #result

选择应该是.result#result

回答by Matt Healy

Try changing <div class="result"></div>to <div id="result"></div>, because that's what you're referencing in your ajax success function

尝试更改<div class="result"></div><div id="result"></div>,因为这就是您在 ajax 成功函数中引用的内容

$("#result").html(server_response);

$("#result").html(server_response);