0

I have a simple ajax request. It calls a PHP page and makes a connection to a MySQL database.

Even if the connection fails (because I turn off the MySQL service), the success part of my ajax call gets triggered...

$.ajax(
            {
                type: "POST", 
                url: "ajax.php",
                timeout: 10000,  

                data: (
                {
                    "type": "approveAndEmail",
                    "id": id                    
                }), 

                error: function(){
                    alert('error'); 
                    },

                success: function(response)
                {
                    alert('yes');
                        }
            });

Any ideas?

My PHP looks like this (and yes I will be updating from mysql_connect!

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
Jeff
  • 6,895
  • 1
  • 15
  • 33
Jack
  • 65
  • 2
  • 9
  • the success part gets triggered if the server returns a 200 header. So it's only telling you that no errors occured in reaching the script and that the script didn't response with any error-like header (4**, 5**). So you should make sure your script sends an error like that when the connection fails. – Jeff Sep 27 '17 at 21:36
  • 1
    ...cnt: so check for mysqli_error after connecting, either continue or send error (a http header 500 fe) back to client (best with a error message that you can read in `error: function(error) {}` – Jeff Sep 27 '17 at 21:39
  • marking as duplicate is helpful, but more so if the notification linked to the answer to this question. My searching didn't find it. The posts here have helped enormously and it's now working. – Jack Sep 28 '17 at 04:52
  • combining header("HTTP/1.1 500 Internal Server Error"); with the fail / done method has worked for me. – Jack Sep 28 '17 at 04:53

1 Answers1

1

You could resolve this by throwing an exception if the MySql connection fails, like so,

$serverConn= mysql_connect("$host", "$username", "$password");
$dbConn = mysql_select_db("$db_name");

if (!$serverConn || !$dbConn) {
      throw new Exception("Database Connection Failed");
}

By throwing the exception, an internal server error will occur, which will be detected by the AJAX fail callback.

Be sure to modify your ajax call like this:

$.ajax(
            {
                type: "POST", 
                url: "ajax.php",

                data: (
                {
                    "type": "approveAndEmail",
                    "id": id                    
                }), 
            }).done(function(data) {
                    alert("Call Worked")
            })
            .fail(function(data) {
                alert("error happened(DB)")
            })

the .fail call seems to capture internal server errors better.

Let me know if that works.

Alexander Edwards
  • 772
  • 2
  • 7
  • 18
  • Thanks for the response. I've tried this, but the .done() triggers even if the database connection fails! it doesn't seem to pick up the exception that is thrown. – Jack Sep 28 '17 at 04:34
  • I've wrapped it in a try {} catch {} and this seems to work... ? Was it supposed to work without this? – Jack Sep 28 '17 at 04:45