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
Is this possible? Something like this
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);
})
}
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));
};
.done()with different data types as the (return) dataType is set before making the ajax call. In your second (json).done()you could JSON.parse(data). Butdatain the first is html. How will your single end point with a single call return two different data types? It's not jquery that determines the data type, it's the server; jquery just converts it for you (eg parses json if you say it's json that's being returned, so you get an object instead of a json string)One is a data type JSON, the other is not.... One ajax call = one response having one data type. You should do 2 request to two diferent endpoint to have two different results.