jquery ajax post成功返回数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6007458/
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
jquery ajax post success return data
提问by cj333
I can not get back my data, here is my code. where is the problem? Thanks.
我无法取回我的数据,这是我的代码。问题出在哪儿?谢谢。
Index.php
索引.php
<script type="text/javascript">
jQuery(document).ready(function(){
$(".click").click(function(){
var value = $(this).val();// post each click value to another page
$.ajax({
url: "post.php",
dataType: "html",
type: 'POST', //I want a type as POST
data: "name="+value,
success: function(data){
$("#result").data($data);
}
});
});
});
</script>
<div id="result"></div>
<a href="#" class="click">tim</a>
<a href="#" class="click">tom</a>
<a href="#" class="click">jimmy</a>
post.php
后.php
<?php
$name=trim(addslashes(htmlspecialchars(rawurldecode($_POST["name"]))));
$data .='Your name is '.$name;
$data .='how do you do';
echo $data;// how to return all the html in post.php? or return $data part?
?>
回答by Tatu Ulmanen
See the problem?
看到问题了吗?
...
success: function(data){
$("#result").data($data);
}
...
You take the data as data
but try to access it as $data
, which is a different, uninitialized variable.
您将数据视为,data
但尝试将其访问为$data
,这是一个不同的未初始化变量。
Also, you cannot use .val()
on an a
element, use .html()
instead to get the inner HTML. You probably want to use .html()
instead of .data()
on #result
also.
此外,您不能.val()
在a
元素上使用,.html()
而是使用来获取内部 HTML。您可能还想使用.html()
而不是.data()
on #result
。
Otherwise your example seems all right.
否则你的例子似乎没问题。
回答by Shiv Om Sharma
it should be:
它应该是:
success: function(data) {
$("#result").html(data);
}
回答by Chris Fulstow
Looks like you've got an extra dollar sign on the $data
variable, should be:
看起来你在$data
变量上有一个额外的美元符号,应该是:
success: function(data) {
$("#result").data(data);
}