Skip to content

Instantly share code, notes, and snippets.

@bordoni
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save bordoni/34384a519b7c310d74be to your computer and use it in GitHub Desktop.

Select an option

Save bordoni/34384a519b7c310d74be to your computer and use it in GitHub Desktop.
$.ajax({
'url': ajaxurl,
'data': {
'action': data.crop_action,
'crop': data.crop,
'attachment': data.$.return.val()
},
'dataType': 'json',
'type': 'POST',
'success': function (response, textStatus, jqXHR){
if (typeof response !== 'object')
response = { 'status': false, 'message': data.error };
console.log(response);
if (!response.status)
return data.$.uploader.trigger('flag.cropper', ['is_empty']).trigger('unflag.cropper', ['is_loading', response.message]);
data.$.img = $('<img>').attr('src', response.image.url + "?r=" + parseInt(new Date().getTime()/1000, 10));
data.$.img.on({
'load': function() {
$(this).siblings('img').remove();
data.$.uploader.trigger('unflag.cropper', ['is_loading']).trigger('flag.cropper', ['is_loaded'])
}
}).appendTo(data.$.images);
},
'error': function (jqXHR, textStatus, errorThrow){
return data.$.uploader.trigger('unflag.cropper', ['is_loading', data.error]).trigger('flag.cropper', ['is_empty']);
},
'complete': function (jqXHR, textStatus){
}
});
<?php
add_action('wp_ajax_javascript_action_value', "ajax_func_action");
add_action('wp_ajax_nopriv_javascript_action_value', 'ajax_func_action');
function ajax_func_action() {
if ( ! defined( 'DOING_AJAX' ) ) {
return;
}
$response = (object) array(
'status' => false,
'message' => __("Houve um erro na requisição"),
);
$request = (object) array(
'width' => absint($_POST['width']),
'height' => absint($_POST['height'])
);
check_ajax_referer( "javascript_nonce_value" );
$response->image = (object) array(
'url' => esc_url($image[0]),
'width' => absint($image[1]),
'height' => absint($image[2]),
);
$response->ratio = $response->image->width/$response->image->height;
exit(json_encode($response));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment