1

I changed the permalinks from /%category%/%postname%/ to just /%postname%/ and now my custom ajax function returns a 500 error. The exact message I get in the console is:

Failed to load resource: the server responded with a status of 500 () /wp-admin/admin-ajax.php:1

Even if I change it back to /%category%/%postname%/ it still doesnt work.

I have made no other changes to the ajax javascript or the underlying PHP script that sends results to the ajax function.

I read in another post that the URL to the admin-ajax.php has changed and that I need to update it in my functions for the ajax. But Im not sure what/if its changed or changed to.

Here is the PHP that enques the ajax scripts:

function my_script_enqueuer() {
   wp_register_script( "snek_results", get_template_directory_uri() . '-child/js/results.js', array('jquery') );
   wp_localize_script( 'snek_results', 'snek_results', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        

   wp_enqueue_script('jquery', false, array(), false, false);
   wp_enqueue_script( 'snek_results');

}

Here is the AJAX functions:

jQuery(document).ready(function($) {
  $('#game').on('submit', function(event) {
    event.preventDefault(); // Prevent the form from submitting the traditional way

    // Capture the values from the selected radio buttons for choice and bet
    var choice = $("input[name='choice']:checked").val();
    var bet = $("input[name='bet']:checked").val();
    
    if (choice && bet) {
      // Now call your AJAX function with the user's input
      getDiceRollResults(choice, bet);
    } else {
      $('#results').html('Please select both a choice and a bet');
    }
  });

  function getDiceRollResults(choice, bet) {
    $.ajax({
      url: snek_results.ajaxurl, // Use localized script object
      type: 'POST',
      data: {
        action: 'roll_dice',
        choice: choice,
        bet: bet,
      },
      success: function(response) {
        var jsonResponse = JSON.parse(response);
        if (jsonResponse.status === 'winner') {
          $('#results').html('You won! Your prize is ' + jsonResponse.prize);
        } else if (jsonResponse.status === 'loser') {
          $('#results').html('You lost! Try again.');
        } else if (jsonResponse.status === 'error') {
          $('#results').html('Error: ' + jsonResponse.message);
        }
      },
      error: function() {
        $('#results').html('AJAX error');
      },
    });
  }
});

Everything worked fine until I messed with the permalinks.

Thanks!

1 Answer 1

0

Have you already tried to flush permalinks? See https://www.wpbeginner.com/beginners-guide/how-to-regenerate-your-permalinks-in-wordpress/

Then I would start debugging the value of snek_results.ajaxurl

3
  • Hi, Ive done the flush permalinks a few times and nothing changes. I know nothing about how to debug javascript so if you could give me some pointers or suggestions of what im looking for that would help. as far as i can tell from the console in chrome, the url to the admin-ajax.php is correct.
    – jon
    Sep 25, 2023 at 23:43
  • update: using the wordpress debug features i did find a problem with the way i called the function wp_localize_script and i fixed that but that didnt solve the ajax error.
    – jon
    Sep 26, 2023 at 2:50
  • In javascript you can use debug.log() function to print things in the console. I would start by checking the value of snek_results.ajaxurl, so for example add console.log(snek_results.ajaxurl); at the beginning of the function getDiceRollResults and see if the value is defined and makes sense with your code. In my opinion you didn't break the ajax call when you changed the permalink, those two have nothing in common. If the ajax calls the correct url you need to check if you are entering in your function and if so understand where it fails.
    – alula
    Sep 27, 2023 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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