我不明白 success 函数在使用 jquery 的 $.ajax 调用中是如何工作的。
例如
$.ajax({
type: "POST",
url: "ajax.php",
data: "function=1",
success: function(data,response,jqxhr){
useReturnData(data /* ??? not sure how to use the data var */ );
}
};
ajax.php:
<?php
if ($_REQUEST['function'] == '1'){
$string = "this is the data i want to return and use";
}
?>
我如何在成功功能中使用该数据?似乎没有地方解释数据参数是什么,他们似乎只是模棱两可地使用它。
另外一个问题,data: "function=1"是否与作为成功函数参数的数据相关?
最佳答案
data
变量包含您的 php 文件的输出,因此如果您在 php 文件中执行以下操作:
echo "<p>success</p>";
data
将包含 <p>success</p>
.
在您的示例中,您会将 php 文件更改为:
<?php
if ($_REQUEST['function'] == '1'){
$string = "this is the data i want to return and use";
}
// other stuff...
echo $string;
?>
关于javascript - 我如何通过 $.ajax 调用使用成功函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8979350/