最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax call not getting a success response - Stack Overflow

programmeradmin6浏览0评论

We have 2 text fields. We have tried to post user_Name and Password through an ajax call.

We have used PHP services and the username and password do save to the DB successfully.

But we do not get any response from our success or fail.

The code:

PHP:

 if(isset($_GET['ecovauserName']) && isset($_GET['ecovauserage']) ){
    $ecovauserName             = $_GET['ecovauserName'];
    $ecovauserage              = $_GET['ecovauserage'];



     $sql  = "SELECT ecovauserName,ecovauserage FROM ecovauserinfo WHERE ecovauserName = '" . $ecovauserName . "' and ecovauserage = '" . $ecovauserage . "'";
    $query      = mysql_query($sql);

    if (mysql_num_rows($query) > 0)
    {
        echo "fail to post";
    }
    else
        {  echo("Entered into DB inserting the values");

        $insert = "INSERT INTO ecovauserinfo (ecovauserName,ecovauserage)
                        VALUES ('" . $ecovauserName . "','" . $ecovauserage . "')";

        $query  = mysql_query($insert);
        echo $insert;
        if ($query)
        {
            echo "EventFile Successfully stored";
        }
            else
            {


                echo "Insert failed";
            }
        }
     }

Ajax Call:-

 $.ajax({
               url:'http://192.168.3.134:8080/ekova/postevent.php',
               type:'POST',
               data:{ecovauserName :username,ecovauserage:password},
               contentType: "application/json; charset=utf-8",
                dataType: "jsonp",
               success:function(responsive){
                alert(responsive);
               },
               error:function(w,t,f){
                 console.log(w+' '+t+' '+f);
               }
            });

The above code is working fine. The username and password are successfully stored in the DB. But we need to get a success of fail response.

My success:function is not called and so my alert box never runs to notify me of the success.

Please guide me with what is wrong in the code.

We have 2 text fields. We have tried to post user_Name and Password through an ajax call.

We have used PHP services and the username and password do save to the DB successfully.

But we do not get any response from our success or fail.

The code:

PHP:

 if(isset($_GET['ecovauserName']) && isset($_GET['ecovauserage']) ){
    $ecovauserName             = $_GET['ecovauserName'];
    $ecovauserage              = $_GET['ecovauserage'];



     $sql  = "SELECT ecovauserName,ecovauserage FROM ecovauserinfo WHERE ecovauserName = '" . $ecovauserName . "' and ecovauserage = '" . $ecovauserage . "'";
    $query      = mysql_query($sql);

    if (mysql_num_rows($query) > 0)
    {
        echo "fail to post";
    }
    else
        {  echo("Entered into DB inserting the values");

        $insert = "INSERT INTO ecovauserinfo (ecovauserName,ecovauserage)
                        VALUES ('" . $ecovauserName . "','" . $ecovauserage . "')";

        $query  = mysql_query($insert);
        echo $insert;
        if ($query)
        {
            echo "EventFile Successfully stored";
        }
            else
            {


                echo "Insert failed";
            }
        }
     }

Ajax Call:-

 $.ajax({
               url:'http://192.168.3.134:8080/ekova/postevent.php',
               type:'POST',
               data:{ecovauserName :username,ecovauserage:password},
               contentType: "application/json; charset=utf-8",
                dataType: "jsonp",
               success:function(responsive){
                alert(responsive);
               },
               error:function(w,t,f){
                 console.log(w+' '+t+' '+f);
               }
            });

The above code is working fine. The username and password are successfully stored in the DB. But we need to get a success of fail response.

My success:function is not called and so my alert box never runs to notify me of the success.

Please guide me with what is wrong in the code.

Share Improve this question edited Mar 6, 2015 at 12:52 samayo 16.5k13 gold badges94 silver badges113 bronze badges asked Mar 6, 2015 at 10:15 Pavan AlapatiPavan Alapati 2673 gold badges5 silver badges15 bronze badges 1
  • 1 Danger: You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. – Quentin Commented Mar 6, 2015 at 10:40
Add a ment  | 

5 Answers 5

Reset to default 1

Why don't you use status codes returned by the call, as an example:

$.ajax({
  url:'http://192.168.3.134:8080/ekova/postevent.php',
  type:'POST',
  data:{ecovauserName :username,ecovauserage:password},
  contentType: "application/json; charset=utf-8",
  dataType: "jsonp",
  statusCode: {
    404: function () {
      //error
    },
    200: function (data) {
      //response data
    }
  }
});

You could try removing the line dataType: "jsonp" - that's had some success.

See this post: Ajax success event not working

EDIT - try putting basic console.log statements in both success and fail just to see if either are being hit.

$.ajax({
           url:'http://192.168.3.134:8080/ekova/postevent.php',
           type:'POST',
           data:{ecovauserName :username,ecovauserage:password},
           contentType: "application/json; charset=utf-8",
            dataType: "jsonp",
           success:function(){
            console.log('in success');
           },
           error:function(){
             console.log('in error');
           }
        });

Check whether you're getting an error:

error: function(xhr, status, error) {
  var error1 = eval("(" + xhr.responseText + ")");
  console.log(error1.Message);
  console.log(geturl.getAllResponseHeaders());
  alert("error!"+ geturl.getAllResponseHeaders());
}

The data type should read "json" not "jsonp"

I would suggest that you use "text" instead of "json" since you are returning normal text results and not json encoded data.

Return a json_encode response in your php call, like this:

echo json_encode(array('status' => 'in success'));

You seems doing a query with cross-domain and jsonp make that possible.

So, in your php code you have to return a jsonp format :

$response = array('success' => false, 'message' => '');

if (isset($_GET['ecovauserName']) && isset($_GET['ecovauserage'])) {
    $ecovauserName = $_GET['ecovauserName'];
    $ecovauserage = $_GET['ecovauserage'];

    $sql = "SELECT ecovauserName,ecovauserage FROM ecovauserinfo WHERE ecovauserName = '" . $ecovauserName . "' and ecovauserage = '" . $ecovauserage . "'";
    $query = mysql_query($sql);

    if (mysql_num_rows($query) > 0) {
        $response['message'] = 'fail to post';
    } else {
        $insert = "INSERT INTO ecovauserinfo (ecovauserName,ecovauserage) VALUES ('" . $ecovauserName . "','" . $ecovauserage . "')";

        $query = mysql_query($insert);
        if ($query) {
            $response['message'] = 'EventFile Successfully stored';
            $response['success'] = true;
        } else {
            $response['message'] = 'Insert failed';
        }
    }
}
// The header is for telling that you are sending a json response
header('content-type: application/json; charset=utf-8');

// formatting a response as jsonp format.
echo $_GET['callback'] . '(' . json_encode($response) . ')';

In your javascript :

$.ajax({
    url: 'http://192.168.3.134:8080/ekova/postevent.php',
    type: 'POST',
    data: {ecovauserName: username, ecovauserage: password},
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: function (response) {
        alert(response.message);
    },
    error: function() {
        console.log('error');
    }
});

More information at http://www.geekality/2010/06/27/php-how-to-easily-provide-json-and-jsonp

发布评论

评论列表(0)

  1. 暂无评论