jquery ajax post成功返回数据
我无法取回我的数据,这是我的代码。问题出在哪里?谢谢。
索引.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>
帖子.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?
?>
I can not get back my data, here is my code. where is the problem? Thanks.
Index.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
$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?
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看到问题了吗?
您将数据作为
data
获取,但尝试将其作为$data
访问,这是一个不同的、未初始化的变量。另外,您不能在
a
元素上使用.val()
,而是使用.html()
来获取内部 HTML。您可能还想在#result
上使用.html()
而不是.data()
。否则你的例子看起来没问题。
See the problem?
You take the data as
data
but try to access it as$data
, which is a different, uninitialized variable.Also, you cannot use
.val()
on ana
element, use.html()
instead to get the inner HTML. You probably want to use.html()
instead of.data()
on#result
also.Otherwise your example seems all right.
应该是:
it should be:
看起来
$data
变量上有一个额外的美元符号,应该是:Looks like you've got an extra dollar sign on the
$data
variable, should be: