简体   繁体   中英

JQuery AJAX Always Success message -

Regardless of what I type in my edit_data.php file, I always get a success message. I can't setup error or, in this case, success handling as it always says my variables are undefined.

How do I setup error handling if all my variables keep erroring out with undefined?

function UpdateDB() {

        // DEBUG
        alert('UPDATED DB!');

        //Hide All Alerts
        $('.alert').hide();

        //Show processing message.
        $('#processing_alert').fadeIn(1000);            

        // Run MySQL Script
        $.ajax({
          url: 'edit_data.php',

          success: function(data, status, error) {

            // IF SUCCESSFUL

            //Hide All Alerts
            $('.alert').hide();

            //Show processing message.
            $('#databaseS_alert').fadeIn(1000);
            $('#generated_msg_placehold').after('<h4>Original Request: ' + $data + '</h4>');
            $('#generated_msg_placehold').after('<h4>Status: ' + $status + '</h4>');
            $('#generated_msg_placehold').after('<h4>Error: ' + $error + '</h4>');

          },

          error: function(data, status, error) {

            // IF FAILED

            //Hide All Alerts
            $('.alert').hide();

            //Show processing message.
            $('#databaseF_alert').fadeIn(1000);
            $('#generated_msg_placehold').after('<h4>Original Request: ' + $data + '</h4>');
            $('#generated_msg_placehold').after('<h4>Status: ' + $status + '</h4>');
            $('#generated_msg_placehold').after('<h4>Error: ' + $error + '</h4>');

          }

        });     

}

Any suggestions?

You don't need dollar signs before variables in Javascript. That's probably why they're all coming up as undefined. Should be,

      success: function(data, status, error) {

        // IF SUCCESSFUL

        //Hide All Alerts
        $('.alert').hide();

        //Show processing message.
        $('#databaseS_alert').fadeIn(1000);
        $('#generated_msg_placehold').after('<h4>Original Request: ' + data + '</h4>');
        $('#generated_msg_placehold').after('<h4>Status: ' + status + '</h4>');
        $('#generated_msg_placehold').after('<h4>Error: ' + error + '</h4>');

      },

      error: function(data, status, error) {

        // IF FAILED

        //Hide All Alerts
        $('.alert').hide();

        //Show processing message.
        $('#databaseF_alert').fadeIn(1000);
        $('#generated_msg_placehold').after('<h4>Original Request: ' + data + '</h4>');
        $('#generated_msg_placehold').after('<h4>Status: ' + status + '</h4>');
        $('#generated_msg_placehold').after('<h4>Error: ' + error + '</h4>');

      }

The success/fail from the AJAX standpoint is based on the HTTP response received for the request. A 200 response is a success. So even if you PHP script sent out a message like Epic fail , unless you also modify the headers to also return some sort of error header (ie 4XX or 5XX response code), you will always engage the success handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM