jquery在同一个函数中使用两个已完成的回调,一个带有数据类型json,另一个没有

jquery在同一个函数中使用两个已完成的回调,一个带有数据类型json,另一个没有,jquery,ajax,Jquery,Ajax,尝试在jQuery函数中使用两个done回调。一个是数据类型JSON,另一个不是。 第一个函数为其数据调用一个php函数,该函数不是JSON数组。第二种方法是从同一个php调用调用JSON编码的数组。我需要将ID的数组传递给另一个函数 这可能吗?像这样的 function func1() { jQuery.ajax({ type: 'post', url: my_ajax.ajax_url, data: { action: 'myphp' }

尝试在jQuery函数中使用两个
done
回调。一个是数据类型JSON,另一个不是。 第一个函数为其
数据
调用一个php函数,该函数不是JSON数组。第二种方法是从同一个php调用调用JSON编码的数组。我需要将ID的数组传递给另一个函数

这可能吗?像这样的

function func1() {

  jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    data: {
      action: 'myphp'
    }
  })

  .done(function(data) {
    jQuery('#adivID').html(data);
  })
  .fail(function(xhr, status, error) {
    console.log(xhr.responseText);
    alert(error);
  })

  dataType: 'JSON',
  .done(function(data) {
    ids: id;
    func2(data.ids);
  })
}
编辑1-尝试两个端点

function func1() {
  jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    data: {
      action: 'myphp'
    }
  })

  .done(function(data) {
    jQuery('#adivID').html(data);
  })
  .fail(function(xhr, status, error) {
    console.log(xhr.responseText);
    alert(error);
  })

  jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    dataType: 'JSON',
    data: {
        action: 'myphp',
        ids: id;
    }
  })   
  .done(function(data) {
    func2(data.ids);
  })
}
PHP


是的,您可以嵌套ajax回调。但首先我们可能想回顾一下我们在这里讨论的内容

JSON
代表
javascript对象表示法
。它不是一种数据类型,而是一种将javascript对象编码为文本字符串的方法,因此可以方便地存储/传输。

jQuery
是一个javascript库。它基本上是香草糖

PHP
是一种服务器端语言,用于接收客户端请求、处理请求并返回响应。您不能从客户端调用php函数。您提出请求,服务器决定如何响应

另外,如果您希望从同一url返回不同的数据,则必须将其添加到options对象中,然后处理该服务器端。客户端应该是这样的:

jQuery.post(my_ajax.ajax_url, { action: 'myphp', return_type: 'string' })
  .done(function(data) { //data will be a string
    jQuery('#adivID').html(data);

    //this part doesn't make sense, because if the data is a string,
    //it won't have an 'id' property. But maybe in your actual code this
    //does make sense. So this is just an example of how nesting works.
    jQuery.getJSON(my_ajax.ajax_url, { ids: data.ids, return_type: 'json' })
      .done(function(json) { //json will be an object
        //do stuff with the object
      })
      .fail(function() {
        //response was probably not properly json formatted
      });
  })
  .fail(function(xhr, status, error) {
    console.log(xhr.responseText);
    alert(error);
  });
$ids = $wpdb->get_results("SELECT id FROM mycats", ARRAY_N); //returns an indexed array of ids

switch($_POST["return_type"]) {
  case "string": //returns a comma separated string of ids
    echo implode(", ", $ids);
    break;
  case "json": //returns a json array of ids
    echo json_encode($ids);
    break;
}
它们只需要很少的人手,因为它们需要更少的参数,而且除非您正在做更复杂的事情,否则会使代码更易于阅读

编辑(因为您添加了php代码):

我对wordpress不熟悉,读了几遍之后,我才明白你的代码在做什么。但我从中得到的信息是,你可能想要这样的东西:

jQuery.post(my_ajax.ajax_url, { action: 'myphp', return_type: 'string' })
  .done(function(data) { //data will be a string
    jQuery('#adivID').html(data);

    //this part doesn't make sense, because if the data is a string,
    //it won't have an 'id' property. But maybe in your actual code this
    //does make sense. So this is just an example of how nesting works.
    jQuery.getJSON(my_ajax.ajax_url, { ids: data.ids, return_type: 'json' })
      .done(function(json) { //json will be an object
        //do stuff with the object
      })
      .fail(function() {
        //response was probably not properly json formatted
      });
  })
  .fail(function(xhr, status, error) {
    console.log(xhr.responseText);
    alert(error);
  });
$ids = $wpdb->get_results("SELECT id FROM mycats", ARRAY_N); //returns an indexed array of ids

switch($_POST["return_type"]) {
  case "string": //returns a comma separated string of ids
    echo implode(", ", $ids);
    break;
  case "json": //returns a json array of ids
    echo json_encode($ids);
    break;
}

但是同样地,
getJSON()
将失败,因为
post()
将返回一个字符串。在不确切知道您想要完成什么的情况下,很难推荐代码。可能还值得注意的是,在php中,an不同于an,也不同于an。此外,所有变量都以
$
开头<代码>wpdb!=
$wpdb
如果您没有因此损失几个小时,那么您就没有编写足够的php哈哈。

是的,您可以嵌套ajax回调。但首先我们可能想回顾一下我们在这里讨论的内容

JSON
代表
javascript对象表示法
。它不是一种数据类型,而是一种将javascript对象编码为文本字符串的方法,因此可以方便地存储/传输。

jQuery
是一个javascript库。它基本上是香草糖

PHP
是一种服务器端语言,用于接收客户端请求、处理请求并返回响应。您不能从客户端调用php函数。您提出请求,服务器决定如何响应

另外,如果您希望从同一url返回不同的数据,则必须将其添加到options对象中,然后处理该服务器端。客户端应该是这样的:

jQuery.post(my_ajax.ajax_url, { action: 'myphp', return_type: 'string' })
  .done(function(data) { //data will be a string
    jQuery('#adivID').html(data);

    //this part doesn't make sense, because if the data is a string,
    //it won't have an 'id' property. But maybe in your actual code this
    //does make sense. So this is just an example of how nesting works.
    jQuery.getJSON(my_ajax.ajax_url, { ids: data.ids, return_type: 'json' })
      .done(function(json) { //json will be an object
        //do stuff with the object
      })
      .fail(function() {
        //response was probably not properly json formatted
      });
  })
  .fail(function(xhr, status, error) {
    console.log(xhr.responseText);
    alert(error);
  });
$ids = $wpdb->get_results("SELECT id FROM mycats", ARRAY_N); //returns an indexed array of ids

switch($_POST["return_type"]) {
  case "string": //returns a comma separated string of ids
    echo implode(", ", $ids);
    break;
  case "json": //returns a json array of ids
    echo json_encode($ids);
    break;
}
它们只需要很少的人手,因为它们需要更少的参数,而且除非您正在做更复杂的事情,否则会使代码更易于阅读

编辑(因为您添加了php代码):

我对wordpress不熟悉,读了几遍之后,我才明白你的代码在做什么。但我从中得到的信息是,你可能想要这样的东西:

jQuery.post(my_ajax.ajax_url, { action: 'myphp', return_type: 'string' })
  .done(function(data) { //data will be a string
    jQuery('#adivID').html(data);

    //this part doesn't make sense, because if the data is a string,
    //it won't have an 'id' property. But maybe in your actual code this
    //does make sense. So this is just an example of how nesting works.
    jQuery.getJSON(my_ajax.ajax_url, { ids: data.ids, return_type: 'json' })
      .done(function(json) { //json will be an object
        //do stuff with the object
      })
      .fail(function() {
        //response was probably not properly json formatted
      });
  })
  .fail(function(xhr, status, error) {
    console.log(xhr.responseText);
    alert(error);
  });
$ids = $wpdb->get_results("SELECT id FROM mycats", ARRAY_N); //returns an indexed array of ids

switch($_POST["return_type"]) {
  case "string": //returns a comma separated string of ids
    echo implode(", ", $ids);
    break;
  case "json": //returns a json array of ids
    echo json_encode($ids);
    break;
}

但是同样地,
getJSON()
将失败,因为
post()
将返回一个字符串。在不确切知道您想要完成什么的情况下,很难推荐代码。可能还值得注意的是,在php中,an不同于an,也不同于an。此外,所有变量都以
$
开头<代码>wpdb!=
$wpdb
如果你没有因此损失几个小时,那么你就没有编写足够的php哈哈。

你问题中的代码在语法上是无效的。你能把它修好吗?添加适当的缩进也会使代码更具可读性运行代码进行格式化。它不会修复你的坏代码,谢谢。EditedLooks在我看来,OP正在发布代码片段(不是那么多的片段),试图展示他们试图实现的目标
一个是数据类型JSON,另一个不是。
。。。一个ajax调用=一个响应具有一种数据类型。您应该对两个不同的端点执行2个请求,以获得两个不同的结果。问题中的代码在语法上无效。你能把它修好吗?添加适当的缩进也会使代码更具可读性运行代码进行格式化。它不会修复你的坏代码,谢谢。EditedLooks在我看来,OP正在发布代码片段(不是那么多的片段),试图展示他们试图实现的目标
一个是数据类型JSON,另一个不是。
。。。一个ajax调用=一个响应具有一种数据类型。您应该向两个不同的端点发出2个请求,以获得两个不同的结果。感谢您发布了一个深思熟虑的回答。当尝试使用上述示例时,我收到了一个“数据未定义”错误?考虑到这与我习惯的方式有很大不同,我不确定这告诉了我什么,除了显而易见的——数据没有定义。错误来自第二个嵌套调用,其中显示的是
数据。ids
似乎第二部分在第一个想法之前运行感谢您发布了深思熟虑的答案在尝试使用上述示例时,我收到了“数据未定义”错误?考虑到这与我习惯的方式有很大不同,我不确定这告诉了我什么,除了显而易见的——数据没有定义。错误来自第二个嵌套调用,其中显示
data.ids
似乎第二部分在第一部分之前运行