views:

60

answers:

1

I have a couple AJAX requests being used in a theme options page. Both worked just fine before, but now the first works and the second fails every time - the failure looks like this in Firebug: http://cl.ly/1w5u and this is in Webkit: cl.ly/1wYn. I'm not getting ANY response of any kind. The thing is, the first one works just fine.

Here's the JS (jQuery) that submits:

jQuery('.cropimage').click(function(){
 var pid = jQuery('#tump_header_img').val();
 var path = jQuery('#header_img_edit .container img').attr('src');
 var dimensions = jQuery('#tump_header_img_position').val();

 var security = jQuery('#_ajax_nonce_crop_apply_image').val();

 jQuery.post(ajaxurl, {pid: pid, action: 'crop_apply_header_image', path: path, dimensions: dimensions, security: security}, function(response) {
  console.log(response);
 });
});

ajaxurl is correct, it's the exact same URL used in the request that does work. I've tried stripping out everything and just trying to get a response, to no avail.

The good stuff in functions.php:

    add_action('wp_ajax_crop_apply_header_image', 'crop_apply_header_image');

    function crop_apply_header_image() {
     check_ajax_referer('crop_apply_header_image', 'security'); 

     $data = $_POST;
     unset($data['security'], $data['action']);

     $dimensions = explode(',',$data['dimensions']);

     $extension_pos = strrpos($data['path'], '/'); // find position of the last dot, so where the extension starts
     $newpath = substr($data['path'], 0, ($extension_pos +1)) . 'cropped-' . substr($data['path'], ($extension_pos + 1));

     update_option( 'tump_header_img_path', $newpath );

     die( wp_crop_image($data['pid'],$dimensions[0],$dimensions[1],$dimensions[2],$dimensions[3],940,200) );
}

Anyways - it gets to none of this as far as I can tell. I don't know what's wrong, any help is greatly appreciated!

+2  A: 

If wp_crop_image would give an error, would you see it? Have you enabled debugging? Do you see something when you place a var_dump($data) there?

Try intercepting the control flow where it hits your server, at admin-ajax.php. Place a var_dump that only triggers when your code is called:

if ($_REQUEST['action'] == 'crop_apply_header_image') {
   var_dump($_REQUEST);
   die();
}

If you see this, the error happens somewhere in the WordPress code, but there isn't too much of it between that point and your handler function. Your action should trigger at the end of the switch statement, in the default part:

default :
    do_action( 'wp_ajax_' . $_POST['action'] );
    die('0');
    break;
endswitch;

Try printing something from the do_action function (but only if $_REQUEST['action'] == 'crop_apply_header_image'), and go down from there.

If you have XDebug or the Zend Debugger set up (worth the time and troubles), you can step through the code without all these print statements. But for a simple debug like this, it should work.

Jan Fabry
I've tried replacing the contents of that function with var_dump, simple echos, return("hello world"), etc. I cannot get this function do anything whatsoever, so this tells me that it's something between the jQuery request and functions.php - this is admin-ajax.php.
Gavin
I added some suggestions on debugging, maybe you already tried all that?
Jan Fabry
Remove the `check_admin_referer()` call and see if you get a response - if you do, it's a problem with your nonces :p
TheDeadMedic
I did some more debugging. Editing admin-ajax.php is a great idea, so I tried a bunch of things there - including commenting out the entire file and replacing it with a var_dump. Nothing. The request is apparently never even reaching admin-ajax.php, which just leaves me baffled. How is the first request able to get it done?
Gavin
It's coming down to two requests on one page or in one JS file not working, and I have no idea why, but this is the first request (that works): jQuery.post(ajaxurl, {pid: pid, action: 'get_large_attachment', security: security}, function(response) {... So if I copy that and place it where my second request is, so that it's exactly the same, it still fails with no error messages or hints whatsoever. Could it be something to do with Apache? I've tried CHMOD 777 on admin-ajax.php.
Gavin
If it works the first time, I don't think there is an error in your Apache configuration. I also didn't notice that you were using a POST request, so I edited my answer to check `$_REQUEST` instead of `$_GET`. Can you see a request for `admin-ajax.php` in your server logs?
Jan Fabry
I've tried it with REQUEST as well. It returns the var dump for the first request, but again nadda for the second. I'm not sure what logs I'd check to look for a request for admin-ajax.php - I'm running MAMP locally. Nothing in particular shows up in the Apache error log. Tough one, eh? Driving me insane! Thank you for all of your input thus far.
Gavin
Can you see it in the Apache access log?
Jan Fabry
I don't know where the access log is if it's different than the error log. It doesn't show up in the error log, however.
Gavin
Ah, MAMP only has the error log by default, but you can [enable the access log yourself](http://documentation.mamp.info/en/mamp/faq/where-can-i-find-the-logs/how-can-i-enable-the-apache-access-logs). This will log every request, so you can see whether the request for `admin-ajax.php` even reaches the server).
Jan Fabry
Got it, thanks. The log, though, not the issue... the log remains blank no matter what I do within a browser. I've ensured the log has the same permissions as the error log.. seems like nothing's being written to it, I don't know exactly what is supposed to show up. I've restarted the servers and double-checked the httpd.conf change (the log file was created automatically once I enabled it).
Gavin
The access log should contain every request to the server, so if you load one page, it should show that page, but also javascripts, images, style sheets... Everything you see in the "Resources" tab of Safari or the "Network" tab of Firebug should be visible there (unless it's cached by the browser, then some items will not be requested again). If it's empty, some configuration or permission is incorrect. But I think permission errors should show up in the error log (or maybe in the general log via the Console.app?).
Jan Fabry
I can't seem to get it to log that way (or at all for that matter). Still messing around with it.
Gavin