jquery ajax post success return data

拜拜、爱过 提交于 2020-06-10 05:57:06

问题


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

回答1:


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.

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.




回答2:


it should be:

success: function(data) {

    $("#result").html(data);
}



回答3:


Looks like you've got an extra dollar sign on the $data variable, should be:

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


来源:https://stackoverflow.com/questions/6007458/jquery-ajax-post-success-return-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!