简体   繁体   English

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

[英]how do i use the success function with an $.ajax call

I don't understand how the success function works in a $.ajax call with jquery. 我不明白成功函数在jquery的$ .ajax调用中如何工作。

for instance 例如

$.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: ajax.php:

 <?php
      if ($_REQUEST['function'] == '1'){
            $string = "this is the data i want to return and use";
      }
 ?>

how do i use that data within the success function? 如何在成功功能中使用该数据? No where seems to explain what the data parameter is, they just seem to use it ambiguously. 似乎没有地方可以解释数据参数是什么,他们似乎模棱两可。

another side question, is the data: "function=1" related to the data as a parameter for the success function? 另一个问题是,数据:“ function = 1”是否与作为成功函数参数的数据相关?

The content of the data parameter depends on the type of the response. data参数的内容取决于响应的类型。 If the Content-Type is application/json , then it's parsed as JSON. 如果Content-Typeapplication/json ,则将其解析为JSON。 If it's text/html or similar, the content is HTML. 如果是text/html或类似内容,则内容为HTML。 In your case, it looks like you're returning text. 就您而言,您似乎正在返回文本。 If you make your Content-Type header text/plain or similar, then data should just be a string. 如果您将Content-Type标头设置为text/plain或类似text/plain ,则data应仅为字符串。

To answer your second question, the data property for the Ajax request is something different; 为了回答您的第二个问题,Ajax请求的data属性有所不同。 it specifies the request data that is sent. 它指定了发送的请求数据。 In other words, it's the query string if you have a GET request, and the post "form" variables if it's a POST request. 换句话说,如果有GET请求,则为查询字符串;如果为POST请求,则为post“ form”变量。

The data variable contains the output of your php file, so if in your php file you do: data变量包含php文件的输出,因此,如果在php文件中,则可以执行以下操作:

echo "<p>success</p>";

data will contain <p>success</p> . data将包含<p>success</p>

In your example you would change your php file to: 在您的示例中,您将php文件更改为:

<?php
      if ($_REQUEST['function'] == '1'){
            $string = "this is the data i want to return and use";
      }

      // other stuff...

      echo $string;
 ?>

data is whatever is returned by the server side script, so in this case it would be data就是服务器端脚本返回的内容,因此在这种情况下

this is the data i want to return and use

Providing the if() condition is met. 提供满足if()条件的条件。

Nobody really says what data contains because it can contain various different things, although it's always a string. 没人真正说出data包含什么内容,因为尽管它总是一个字符串,但它可以包含各种不同的东西。 Sometimes it's HTML, sometimes it's JSON and sometimes just a return message. 有时是HTML,有时是JSON,有时只是返回消息。

In your case, data will just be a string providing you echo the string out in your server side script . 在您的情况下, data将只是一个字符串, 只要您在服务器端脚本中回显该字符串即可

The easiest way is to load the data into some placeholder element (div?) EG 最简单的方法是将数据加载到某个占位符元素(div?)EG中

$.ajax({
    type: "POST",
    url: "ajax.php",
    data: "function=1",
    success: function(data,response,jqxhr){
         $('div.selector').load(data);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM