我无法取回我的数据,这是我的代码。哪里有问题?谢谢。
索引.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?
?>
最佳答案
看到问题了吗?
...
success: function(data){
$("#result").data($data);
}
...
您将数据作为 data
,但尝试将其作为 $data
访问,这是一个不同的未初始化变量。
此外,您不能在 a
元素上使用 .val()
,而是使用 .html()
来获取内部 HTML。您可能还想在 #result
上使用 .html()
而不是 .data()
。
否则你的例子似乎没问题。
关于jquery ajax post 成功返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007458/