javascript - 我如何通过 $.ajax 调用使用成功函数

标签 javascript php jquery ajax

我不明白 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/

相关文章:

javascript - json jquery中的特定键数

javascript - 如何将变量传递给 jQuery 的 .fadeOut()

javascript - jQuery Autocomplete 快完成了吗?

php - 从数组 php 中删除某些字符串

php - 如何触发在 magento 中收到付款的事件?

php - 上传 PDF 文件 PHP

javascript - 从 jquery 中的所有后代中删除样式属性

javascript - UI 路由器参数 URL 不起作用,仅重定向到/

javascript - Jquery:如何将模式对话框设置为相对于 html 而不是 body 居中显示?

javascript - jquery 获取选定的下拉选项值