0

Please advise as I am new to jQuery. The following code is:

function get(database)
    {   
        var dbValue = database; 
        console.debug("dbvalue read as :", dbValue)
        $.ajax// use of jQuery here
            ({
                type: "POST",
                url: "testconnect.php",
                data: {nameDB: dbValue},
                success: function(data)
                { 
                    alert("Successfully login and connected");
                }
                error: function(data) 
                {
                    alert("Unsuccessfully login and connected");
                }
            });
    return false;
    }

If the data is is 1(true), alert will show "Successfully login and connected". But what if the error is 0, it cannot produce "Unsuccessfully login and connected".

Starx
  • 77,474
  • 47
  • 185
  • 261
user1739825
  • 820
  • 4
  • 10
  • 27
  • 1
    The `error` function is called only if the AJAX request fails. If you get a response from your server (even if it's an 'error' response) you'll always call the `success:` function. –  Jul 05 '13 at 09:09
  • If you want to use the error event, you need to return a response code `500 internal server error` on the server side. Otherwise do the check in the success function. – MrCode Jul 05 '13 at 09:09
  • @MrCode That's correct too Thanks – user1739825 Jul 12 '13 at 02:09

4 Answers4

1

error: function() {} will trigger if there is an error in the AJAX request. If the AJAX request does not succeed then only this function will execute.

You can do what you are trying to by comparing the output on your success function, like this:

success: function(data) { 
   if(data == '1') alert("Successfully login and connected");
   else alert("Unsuccessfully login and connected");
}
Starx
  • 77,474
  • 47
  • 185
  • 261
1

Use this:

success: function (data) {
    if (data == "1") {
        alert("Successfully login and connected");
    } else {
        alert("Unsuccessfully login and connected");
    }
}

Success versus error refers to whether the AJAX request/response process was successful, it doesn't try to interpret the data.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

success and error are the built-in plugin functions just to represent if your ajax request is successfully executed or not. Try to check the value stored in data after returning. For ex. if(data == "0){alert("log in error");} else if(data == "1"){alert("log in success");} else{}

Snehal
  • 1
0

Hi This is the most confusing. I tried experimenting with type string, number and bit. I found the most accurate - the bit type(TRUE and FALSE).

Here's what I found after testing in another file php:-

try
{
    $dbh = new PDO($dsn, $user, $pswd, array(PDO::ATTR_TIMEOUT => "10",// 10 seconds
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); // connect to DB, declared as global variable!
    $firephp->warn("Attempting to login and connect to server is successful.");
    $connect = TRUE;

}

catch(PDOException $err)
{
    $firephp->error("Attempting to use selected database is failed.");
    $alertmsg = $err->getMessage();
    $firephp->log($err);
    $connect = FALSE;

They are workable and I then amend the old code below as shown :-

$.ajax// use of jQuery here
            ({
                type: "POST",
                url: "testconnect.php",
                data: {nameDB: dbValue},
                success: function(connect)// meant for ajax request purposes only, not 500 interval server error! 
                { 
                    console.debug("data read as: ", connect);
                    if(connect)//as true or false as indicated in login.php 
                        alert("Successfully login and connected");
                    else
                        alert("Attempt to login and connect is NOT successful");    


                },

Where the "success" is concerned, It is only applicable to Database whether it is successful or or not, I would use the code as above. Whatever the result, "connect" would be read as '11' as TRUE and '' as FALSE.

This way, it is less confusing and straight forward.

And finally for the error, It ia applicable to ajax errors only, not database's status.

I would code it like :-

error: function(xhr, textStatus, error)// for only ajax errors, nothing to do w 
                {
                    console.debug(xhr.statusText);
                    console.debug(textStatus);
                    console.debug(error);
                }

Thank you for your help. All are working. However I have yet to test Ajax errors. How do I simulate ajax errors?

user1739825
  • 820
  • 4
  • 10
  • 27