I'm creating a wordpress plugin and I use the wp_ajax_[your action] callback. In my code the php return nothing to the javascript (no echo, no return). If the php return no value, the 'data' in 'success: function(data)' is empty so why the 'click' trigger work?
the js:
$(document).one('click', '#publish', function (event) {
event.preventDefault();
jQuery.ajax({
type:"post",
url: ajaxurl,
data: {action: 'save_img_data', imgUrlsArray: iUrlsArray, imgNamesArray: iNamesArray, pid: pl_vars.post_id},
success: function(data) {
$('#publish').trigger( "click" );
}
});
});
the php:
function lmg29_img_data() {
$pid = $_POST['pid'];
$iUrlsArray = $_POST['imgUrlsArray'];
$iNamesArray = $_POST['imgNamesArray'];
if (isset($iUrlsArray) and isset($iNamesArray)) {
update_post_meta( $pid, 'lg29_urls', $iUrlsArray );
update_post_meta( $pid, 'lg29_names', $iNamesArray );
die();
}
}
add_action("wp_ajax_save_img_data", "lmg29_img_data");
as you can see there's no response:
The HTTP response code is 200, so it's considered a successful request. Actual data in the response is not required.
And, to be clear, functions in JS don't have mandatory arguments. There's no error if you call a function with arguments in its' declaration without any arguments provided.
A failure occurs if the response code is, for example, 400-something or 500-something. Like 404 or 503. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
To see how to set a response code read this:
if you make a POST request, there is no data in success.
$.ajax({
url: "/post/url.php",
type: "POST",
data: parameters,
success: successFunction /*no data argument*/,
error: errorFunction
});
Therefore, success is "called" (and your trigger happens) but data means nothing.