0

i am sending ajax request to server to get the database. But if i enter incorrect data (which is to be sent over server) nothing is happening, error function is not working, all i am doing is to verify credentials from the server

here is my code

$.ajax
    ({
        url: "URL",
        type: "GET",
        datatype: "jsonp",

        data: {type: 'login', id: C_id},
        ContentType: "application/json",

        success: function(res) 
        {

            var simpleJson = JSON.parse(res);

            myDB.transaction(function (txe1) 
            {
                for (var i = 0; i < simpleJson.User.length; i++) 
                {
                    var Cli_id= simpleJson.User[i].id;
                    myDB.transaction(function (txe) 
                    {
                        txe.executeSql('CREATE TABLE Client_data(Mobile integer , C_id integer, U_id integer , name text , ip integer )');
                    }); 


                    myDB.transaction(function (txe1) 
                    {

                        var data_ins = 'INSERT INTO Client_data (Mobile,C_id,U_id) VALUES (?,?,?)';
                        txe1.executeSql(data_ins, [p,C_id,U_id]
                        ,function(tx, result)
                        {
                            navigator.notification.alert('Inserted' ,  onSignup, 'Info', 'ok'); 

                        },
                        function(error)
                        {
                            navigator.notification.alert('Already Registered'); 
                        });
                    });
                }
            });
        },
    });
});

my PHP code

<?php
header('Access-Control-Allow-Origin:*');
$conn = mysql_connect("***", "***", "****");
if (!$conn) 
{
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("ekspeser_pro")) 
{
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

if(isset($_GET['type']))
{
    if($_GET['type'] == "login")
    {
        $id=$_GET['id'];    
        $sql = "SELECT  * from client_master WHERE id='$id'";
        $result = mysql_query($sql);                            
        $num_rows = mysql_num_rows($result);
        if($num_rows!=0)
        {
            while($myrow = mysql_fetch_assoc($result)) 
            {
                $recipes[]=$myrow;
            }

            $output = json_encode(array('User' => $recipes));
            echo $output;

        }
        else
        {
             print "invalid key";
        }
    }
    else
    {
        print "invalid login";
    }   
}

    else
    {
        echo "invalid";
    }

mysql_close();


?>
Zeeshan
  • 803
  • 6
  • 15
Sawan
  • 11
  • 6

2 Answers2

0

You should implement the error callback to perform some operation when the request fails. This is how you can implement request failure callback.

$.ajax({
  url: "/save/author",
  type: "POST",
  dataType: "json",
  data: { name: "John", age: "35" },
  success: function (data, status, jqXHR) {
    alert("request succeed");
  },
  error: function (jqXHR, status, err) {
    alert("request failed");
  }
})

as per this example, we are just showing an alert with text request failed. You can implement it accordingly as per your requirement.

Prateek Jain
  • 1,504
  • 4
  • 17
  • 27
  • i have tried it..but its not working cuz i am working on cross doamin and jquery API documentation says "error handler is not called for cross-domain script and cross-domain JSONP requests." – Sawan Sep 10 '16 at 06:16
  • You might want to have a look at @Tommi Gustafsson's http://stackoverflow.com/a/22831262/2911348 – Prateek Jain Sep 10 '16 at 07:58
  • @prateek what's the error code you are getting? Some error code like 401 dont invoke error callback – Gandhi Sep 11 '16 at 04:09
0

If i get you correct,you want to validate the data passed to your url, if i am getting you correct you want to handle,please refer below:

Ajax error function will only be called if the request fails, see http://api.jquery.com/jQuery.ajax/

So if you return any response from your PHP server/API, the error function won't be triggered as The "error" setting of the ajax method is fired when the calls fails in the sending process. Errors like "timeout", "404", etc...

However, you can return a key from your PHP code as below to handle success and error in your ajax code:

$data['error'] = $success ? 0:1;// If success than set error to 0 else 1;

and in AJAX success you can handle it as :

success: function (data, status, jqXHR) {
    if(data.error)
      //do something
    else
      //do something else
}

Let me know if any queries

------EDIT------------------

<?php
header('Access-Control-Allow-Origin:*');
$conn = mysql_connect("***", "***", "****");
if (!$conn) 
{
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("ekspeser_pro")) 
{
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

if(isset($_GET['type']))
{
    if($_GET['type'] == "login")
    {
        $id=$_GET['id'];    
        $sql = "SELECT  * from client_master WHERE id='$id'";
        $result = mysql_query($sql);                            
        $num_rows = mysql_num_rows($result);
        $is_error=0;
        if($num_rows!=0)
        {
            while($myrow = mysql_fetch_assoc($result)) 
            {
                $recipes[]=$myrow;
            }

            $output = json_encode(array('User' => $recipes,'is_error'=>$is_error));
            echo $output;

        }
        else
        {
             $is_error=1;
             $error_message = "Invalid Key";
             $output = json_encode(array('is_error'=>$is_error,'error_message'=>$error_message));
             echo $output;
        }
    }
    else
    {
        $is_error=1;
        $error_message = "Invalid Login";
        $output = json_encode(array('is_error'=>$is_error,'error_message'=>$error_message));
        echo $output;
    }   
}

    else
    {
        $is_error=1;
        $error_message = "Invalid";
        $output = json_encode(array('is_error'=>$is_error,'error_message'=>$error_message));
        echo $output;
    }

mysql_close();


?>

In AJAX Code access it like this :

Check for following in success
if(simpleJson.is_error!=1)
   //do your processing
else
   alert(simpleJson.error_message);

Let me know if anything unclear

Zeeshan
  • 803
  • 6
  • 15
  • got error "Uncaught TypeError: Cannot read property 'is_error' of undefined" – Sawan Sep 10 '16 at 10:51
  • can you console.log the `res` to check if you are getting correct response – Zeeshan Sep 10 '16 at 10:55
  • so you just need to check if is_error is set to 1 If anything unclear share your updated code – Zeeshan Sep 10 '16 at 11:05
  • Above you shown us the log message that has is_error set,so to help you further i need to see what you have done in ajax,so post that code – Zeeshan Sep 12 '16 at 07:28