简体   繁体   English

Ajax调用未获得成功响应

[英]Ajax call not getting a success response

We have 2 text fields. 我们有2个文本字段。 We have tried to post user_Name and Password through an ajax call. 我们尝试通过ajax调用发布user_Name和Password。

We have used PHP services and the username and password do save to the DB successfully. 我们已经使用了PHP服务,用户名和密码确实成功保存到数据库中。

But we do not get any response from our success or fail. 但是我们没有得到任何成功或失败的回应。

The code: 代码:

PHP: 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电话: -

 $.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. 用户名和密码已成功存储在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. 请指导我在代码中出现的问题。

The data type should read "json" not "jsonp" 数据类型应为“json”而不是“jsonp”

I would suggest that you use "text" instead of "json" since you are returning normal text results and not json encoded data. 我建议您使用“text”而不是“json”,因为您返回的是普通文本结果而不是json编码数据。

You could try removing the line dataType: "jsonp" - that's had some success. 您可以尝试删除行dataType:“jsonp” - 这已经取得了一些成功。

See this post: Ajax success event not working 看到这篇文章: Ajax成功事件无效

EDIT - try putting basic console.log statements in both success and fail just to see if either are being hit. 编辑 - 尝试将基本的console.log语句置于成功和失败状态,只是为了查看是否有任何命中。

$.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());
}

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
    }
  }
});

在你的php调用中返回一个json_encode响应,如下所示:

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

You seems doing a query with cross-domain and jsonp make that possible. 您似乎正在使用跨域和jsonp进行查询。

So, in your php code you have to return a jsonp format : 所以,在你的PHP代码中,你必须返回一个jsonp格式:

$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 : 在你的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.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp 有关更多信息, 请访问http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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