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
Ajax response inside a div
提问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 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 success
handler 的混合参数。首先是data
你的脚本返回的,然后是textStatus
. 理论上它可以是“超时”、“错误”、“未修改”、“成功”或“解析器错误”。但是,在success
textStatus 中总是会成功的。但是如果您需要添加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 .result
not #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);