jquery ajax发布成功返回数据

时间:2022-10-08 08:33:49

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

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

3 个解决方案

#1


5  

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访问,这是一个不同的未初始化变量。

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(),而是使用.html()来获取内部HTML。你可能想在#result上使用.html()而不是.data()。

Otherwise your example seems all right.

否则你的例子似乎没问题。

#2


4  

it should be:

它应该是:

success: function(data) {

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

#3


2  

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

看起来你在$ data变量上有一个额外的美元符号,应该是:

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

#1


5  

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访问,这是一个不同的未初始化变量。

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(),而是使用.html()来获取内部HTML。你可能想在#result上使用.html()而不是.data()。

Otherwise your example seems all right.

否则你的例子似乎没问题。

#2


4  

it should be:

它应该是:

success: function(data) {

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

#3


2  

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

看起来你在$ data变量上有一个额外的美元符号,应该是:

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