jQuery在同一函数中使用两个完成的回调,一个具有数据类型json,一个没有 [英] jquery Use two done callbacks in same function, one with datatype json one without

查看:53
本文介绍了jQuery在同一函数中使用两个完成的回调,一个具有数据类型json,一个没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在jQuery函数中使用两个 done 回调.一种是数据类型JSON,另一种则不是.第一个调用它的 data 而不是JSON数组的php函数.我想要的第二个可以从相同的php调用中调用JSON编码的数组.我需要将ID的数组传递给另一个函数

Trying to use two done callbacks in a jQuery function. One is a data type JSON, the other is not. The first calls a php function for its data that is not a JSON array. The second I want would call a JSON encoded array from the same php call. I need to pass the array of the ID's to another function

这可能吗?像这样

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-尝试在两个终点

EDIT 1 - attempt at two end points

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

function phpfunctionname{
$id= wpdb->get_results("SELECT id from mycats");
$ids .='';
foreach($id = $value){
ids=$value-.id;
};
echo json_encode(array('ids' => $ids));
};

推荐答案

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

Yes, you can nest ajax callbacks. But first we may want to review what we're talking about here.

JSON 代表 javascript对象表示法.它不是数据类型,而是一种将javascript对象编码为文本字符串的方式,因此可以轻松地存储/传输它. https://www.w3schools.com/js/js_json_intro.asp

JSON stands for javascript object notation. It isn't a data type, it's a way to encode a javascript object into a text string so it can be easily stored/transferred. https://www.w3schools.com/js/js_json_intro.asp

jQuery 是一个JavaScript库.它基本上是香草javascript的语法糖. https://jquery.com/

jQuery is a javascript library. It's basically sytactical sugar for vanilla javascript. https://jquery.com/

PHP 是一种服务器端语言,用于接收客户端请求,对其进行处理并返回响应.您不从客户端调用php函数.您发出请求,服务器决定如何响应. https://www.php.net/

PHP is a sever side language used for taking client requests, doing stuff with it, and returning a response. You don't call php functions from the client. You make a request and the sever decides how to respond. https://www.php.net/

顺便说一句,如果您想从同一URL返回不同的数据,则必须将其添加到options对象中,然后处理该服务器端.客户端将是这样的:

That out of the way, if you want different data back from the same url, you would have to add that in your options object and then handle that server side. Client side would be something like this:

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);
  });

jQuery.post()

jQuery.post() and jQuery.getJSON() are just short hands for jQuery.ajax(), as they require less parameters, and unless you're doing something more complex, keeps your code more readale.

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

Edit (since you added your php code):

我对wordpress不熟悉,花了几读才能弄清楚您的代码在做什么.但是我从 docs 收集的信息中,您可能想要这样的东西:

I'm not familiar with wordpress, and it took a few reads to try and figure out what your code is doing. But what I gather from the docs, you might want something like this:

$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中,对象是不同的来自关联数组

But again, the getJSON() is going to fail because the post() is going to return a string. It's tough to suggest code without knowing exactly what it is you're trying to accomplish. It's probably also worth noting that in php an object is different from an associative array is different from an indexed array. Also, all variables start with a $. wpdb != $wpdb If you haven't lost several hours because of this, you haven't written enough php haha.

这篇关于jQuery在同一函数中使用两个完成的回调,一个具有数据类型json,一个没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆