div中的Ajax响应

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

  $(function(){ // added $('a.vote').click(function(){ var a_href = $(this).attr('href'); $.ajax({ type: "POST", 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  Up Vote 

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

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

现在我正在尝试实现的是获取我的视图文件中的

侧的server_response值(从控制器发送的值)。

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

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

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

 $.ajax({ type: "POST", url: "contents/hello", data: "id=" + a_href, success: function(data, textStatus) { $(".result").html(data); }, error: function() { alert('Not OKay'); } });​ 
 success: function(server_response) { $(".result").html(server_response); }​ 
// result is the class

选择器应该是#result而不是#result

尝试将

更改为

,因为这是您在ajax成功函数中引用的内容

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