'How to get callback values from php class to ajax .done
I have a class that processes the data of a form that does a submit in ajax.
Everything works and I can store the values to the wp database but I can't figure out how to get the $response->body value back from the class to the ajax .done function.
This is the class that manages the json of the ajax
public function __construct($plugin_name, $version) {
$this->plugin_name = $plugin_name;
$this->version = $version;
add_action('wp_ajax_save_cc_dado_settings', array($this, 'save_cc_dado_settings'));
add_action('wp_ajax_nopriv_save_cc_dado_settings', array($this, 'save_cc_dado_settings'));
}
public function save_cc_dado_settings() {
if (isset($_POST['action']) && $_POST['action'] === 'save_cc_dado_settings') {
parse_str($_POST['dati'], $searcharray);
if ($searcharray['chiave_licenza'] != ''):
$data = array('domain' => get_site_url());
$response = Requests::post('https://api.registroconsensi.it/wp_checker.php', array(), $data);
echo '<pre>';
$parsing_dati = json_decode($response->body, true);
echo '</pre>';
echo 'Chiave Licenza=' . $searcharray['chiave_licenza'];
global $wpdb;
$wpdb->update('wp_options',
array('option_value' => $searcharray['chiave_licenza']),
array('option_name' => 'chiave_licenza'),
array('%s', '%s')
);
return wp_send_json_success($response->body);
else:
echo 'failed';
endif;
}
wp_die();
}
}
and this is my ajax code:
(function ($) {
'use strict';
$(window).load(function () {
$('#form_attivazione').submit(function (event) {
event.preventDefault();
var ajax_form_data2 = $("#form_attivazione").serialize();
$.ajax({
url: ajaxurl_arr.ajax_url, // domain/wp-admin/admin-ajax.php
type: 'post',
data: {
action: 'save_cc_dado_settings',
dati: ajax_form_data2
}
})
.always(function () {
event.target.reset();
})
.done(function (response) {
$("#answare").html(response);
})
.fail(function () {
$(" #answare ").html("<h2>Something went wrong.</h2><br>");
});
});
});
})(jQuery);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
