WordPress.org

Ready to get started?Download WordPress

Forums

Ajax 500 Internal Server Error (2 posts)

  1. Gingah
    Member
    Posted 3 years ago #

    Hi there,

    I am trying to utilize ajax-functionality for a plugin I am creating, but get a 500 Internal Server Error when trying to do so. Here is what I am doing:

    1. User changes something on plugin-page.
    2. The change is registered through JavaScript (jQuery).
    3. a jQuery.Post call is made to a function named ajax_callback, as seen below.
    4. The call tries to send two variables to the function: 'caseType' and 'data'.
    5. ajax_callback checks whether 'caseType' is defined, and if it is proceeds with printing 'data'.

    However, step 5 fails, and only the backfire of ajax_callback is printed. The jQuery.Post call looks like this:

    $.post(ajaxurl, {
      action: "ajax_callback",
      caseType: "1",
      data: array1
    }, function(data) {
      $("#result").html(data);
    }, "text");

    And the axax_callback looks like this:

    add_action('wp_ajax_ajax_callback', 'ajax_callback');
    function ajax_callback() {
      if (!$_POST['caseType']) {exit("caseType is not defined");}
      switch ($_POST['caseType']) {
      case "1":
        $data = $_POST['data'];
        print_r($data);
        break;
      case "2":
        $data = $_POST['data'];
        print_r($data);
        break;
    }

    So basically, 'caseType' defines what to do, and 'data' is passed on. But why am I getting the backfire from saying "caseType is not defined"? As seen by the JavaScript code, case "1" of the ajax_callback should be invoked, and the 'data' printed directly.

    Any ideas on what is going wrong?

  2. Gingah
    Member
    Posted 3 years ago #

    Solution: There is actually nothing wrong with the code above. However, admin-ajax.php has no solution for error handling other than directly linked to itself and the request from JavaScript. Meaning: My error came from the callback function in my php, and though that error is actually printed, it does not end up as the response from admin-ajax.php.

Topic Closed

This topic has been closed to new replies.

About this Topic