Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm writing a plugin with an ajax request and I'm getting a weird problem. It works fine the first few times but then all of a sudden, the data ends up empty or NULL.

This is the code:

/* in functions.php */
add_action( 'wp_ajax_rs-MYajaxAction', 'wpw_display_js_vars');
add_action( 'wp_ajax_nopriv_rs-MYajaxAction', 'wpw_display_js_vars');

function wpw_display_js_vars() {
    var_dump($_POST['uid']);
    //outputs null
    die();
}

And:

/* p_name, user_meta.current_user_id, clicked_post and ajaxurl 
 * are all global defined in another part of the script 
 */
$('.walkthrough-list').find('a').each(function() {
    $(this).click(function() {
        $.post(
            ajaxurl,
            {
                action: "rs-MYajaxAction",
                p: p_name, 
                uid : user_meta.current_user_id,
                tut_name : clicked_post
            },
            function(resp) {
                console.log(resp);
        });// end $.post 
        return false;
});

As a jsfiddle

Is there any reason why data gets "lost" when its posted to admin-ajax.php?

share|improve this question
    
Do you have any Javascript errors? –  s_ha_dum Jan 24 '13 at 20:32
    
No, the data is there but when posted to admin-ajax.php, by the time it gets there, the objects are null! –  Roman Epicnerd Sharf Jan 24 '13 at 21:57
    
But you say it works a few times? Sounds like an intermittent server timeout but it is going to be very hard to debug from the outside, so to speak. Do have access to server logs? If so, try to match up requests to admin-ajax.php with your failures. –  s_ha_dum Jan 24 '13 at 22:01
1  
Read Debugging in WordPress, then enable WP_DEBUG and WP_DEBUG_LOG so you can get a log of errors in wp-content/debug.log –  webaware Jan 24 '13 at 23:23
1  
Is this actually a WordPress-specific question? –  Chip Bennett Jan 25 '13 at 0:55

1 Answer 1

up vote 0 down vote accepted

This stackoverflow answer has a good example of basic ajax posting.

http://stackoverflow.com/questions/5004233/jquery-ajax-post-example

I would try adjusting your code so that the info you want to post is in the data parameter. jQuery isn't my strongest suit, but I was under the impression that the $_POST data came through that parameter.

$('.walkthrough-list').find('a').each(function() {
    $(this).click(function() {

        data = 'p='+p_name+&uid='+user_meta.current_user_id+'&tut_name='+clicked_post;
        $.post(
            ajaxurl,
            {
                action: "rs-MYajaxAction",
                data: data
            },
            function(resp) {
                console.log(resp);
        });// end $.post 
        return false;
});

Granted there is probably a much better way to serialize your data.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.