jQuery ajax发布成功返回数据 [英] jquery ajax post success return data

查看:72
本文介绍了jQuery ajax发布成功返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法取回数据,这是我的代码.问题出在哪儿?谢谢.

I can not get back my data, here is my code. where is the problem? Thanks.

Index.php

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? 
?>

推荐答案

看到问题了吗?

...
success: function(data){ 
    $("#result").data($data); 
}
...

您将数据视为data,但尝试将其作为$data访问,这是一个不同的未初始化变量.

You take the data as data but try to access it as $data, which is a different, uninitialized variable.

此外,您不能在a元素上使用.val(),而是使用.html()来获取内部HTML.您可能还想在#result上使用.html()代替.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.

否则,您的示例似乎还可以.

Otherwise your example seems all right.

这篇关于jQuery ajax发布成功返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆