jquery ajax post成功返回数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6007458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:13:05  来源:igfitidea点击:

jquery ajax post success return data

jqueryajaxpost

提问by cj333

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

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

Index.php

索引.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

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

回答by Tatu Ulmanen

See the problem?

看到问题了吗?

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

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

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

Also, you cannot use .val()on an aelement, use .html()instead to get the inner HTML. You probably want to use .html()instead of .data()on #resultalso.

此外,您不能.val()a元素上使用,.html()而是使用来获取内部 HTML。您可能还想使用.html()而不是.data()on #result

Otherwise your example seems all right.

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

回答by Shiv Om Sharma

it should be:

它应该是:

success: function(data) {

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

回答by Chris Fulstow

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

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

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