admin管理员组

文章数量:1208153

My theme uses a custom action which expects two arguments. I am trying to call the function in an AJAX call but because I am not passing those arguments I end up getting a 500 error.

The action is formatted like this:

function newsletterSignup($id, $source) {
   // code here
}
add_action('wp_ajax_nopriv_newsletter_signup', 'newsletterSignup');
add_action('wp_ajax_newsletter_signup', 'newsletterSignup');

My ajax call looks like this:

$('form').on('submit', function(e) {
    e.preventDefault();
    var $form = $(this),
        fd = new FormData($form[0]);

    fd.append('action','newsletter_signup');
    fd.append('source','homepage');

    $.ajax({
        type: 'POST',
        url: user_object.ajaxurl,
        data: fd,
        processData: false,
        contentType: false,
        dataType: 'json',
        success: function(data, textStatus, jqXHR) {
            // registered
        },
        error: function (request, status, error) {
            //error
        }
    });
});

As mentioned, I am getting a 500 error on this request because the action does not include the $id and $source arguments. Is it possible to pass arguments into the action via JavaScript?

My theme uses a custom action which expects two arguments. I am trying to call the function in an AJAX call but because I am not passing those arguments I end up getting a 500 error.

The action is formatted like this:

function newsletterSignup($id, $source) {
   // code here
}
add_action('wp_ajax_nopriv_newsletter_signup', 'newsletterSignup');
add_action('wp_ajax_newsletter_signup', 'newsletterSignup');

My ajax call looks like this:

$('form').on('submit', function(e) {
    e.preventDefault();
    var $form = $(this),
        fd = new FormData($form[0]);

    fd.append('action','newsletter_signup');
    fd.append('source','homepage');

    $.ajax({
        type: 'POST',
        url: user_object.ajaxurl,
        data: fd,
        processData: false,
        contentType: false,
        dataType: 'json',
        success: function(data, textStatus, jqXHR) {
            // registered
        },
        error: function (request, status, error) {
            //error
        }
    });
});

As mentioned, I am getting a 500 error on this request because the action does not include the $id and $source arguments. Is it possible to pass arguments into the action via JavaScript?

Share Improve this question edited Mar 11, 2022 at 14:30 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Mar 10, 2022 at 23:10 user13286user13286 2272 gold badges12 silver badges29 bronze badges 1
  • a 500 error is a generic something went wrong message from the server, it's not the actual error. You need to look in your PHP error log to find the error message. Also, is there a reason you used the old legacy AJAX API instead of the modern REST API for your AJAX requests? – Tom J Nowell Commented Mar 10, 2022 at 23:39
Add a comment  | 

1 Answer 1

Reset to default 1

You can't pass arguments to wp_ajax_ callbacks. You need to pass the data via the data property of the AJAX request. Then you get those values with $_POST and pass them to a function:

function newsletterSignupAjax() {
   $id     = $_POST['id'];
   $source = $_POST['source'];

    /* Make sure to validate and sanitize those values. */

    newsletterSignup( $id, $source );
}
add_action('wp_ajax_nopriv_newsletter_signup', 'newsletterSignupAjax');
add_action('wp_ajax_newsletter_signup', 'newsletterSignupAjax');

本文标签: Wordpress actionPass arguments into action in an AJAX call