【问题标题】:Ajax call not getting a success responseAjax 调用未获得成功响应
【发布时间】:2015-05-07 22:20:51
【问题描述】:

我们有 2 个文本字段。我们已尝试通过 ajax 调用发布 user_Name 和 Password。

我们已经使用了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 调用:-

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

上面的代码工作正常。用户名和密码已成功存储在数据库中。但是我们需要得到一个成功的失败响应。

我的成功:函数没有被调用,所以我的警告框永远不会运行来通知我成功。

请指导我解决代码中的问题。

【问题讨论】:

标签: javascript php jquery ajax response


【解决方案1】:

为什么不使用调用返回的状态码,例如:

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

【讨论】:

    【解决方案2】:

    您可以尝试删除行 dataType: "jsonp" - 这已经取得了一些成功。

    看到这个帖子:Ajax success event not working

    编辑 - 尝试将基本的 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');
               }
            });
    

    检查您是否收到错误:

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

    【讨论】:

    • 我们尝试删除 jsonp.but 成功方法没有调用
    • 如果您将 console.log("blablabla") 放入成功和失败中,是否会调用其中任何一个? - 答案已编辑以反映。
    • 在成功函数console.log中未调用但值存储在db中请指导我如何调用成功
    • 您可以尝试将数据类型更改为“文本”吗?并在成功以及console.log中加入以下内容:return alert("Testing");
    • 我们放置了 case1) dataType:Text 我们得到 'in fail',case2)dataType:json 我们得到 'in fail' case3) dataType:jsonp 我们得到'in fail' 但只有 case3 数据保存到数据库
    【解决方案3】:

    数据类型应该是“json”而不是“jsonp”

    我建议您使用“text”而不是“json”,因为您返回的是普通文本结果而不是 json 编码数据。

    【讨论】:

    • 感谢您的回复。我们按照您所说的进行了尝试,但是当我们放置 JSON 时,数据未保存在数据库中
    • dataType 用于告诉 ajax 哪些数据需要返回而不是发送。因此,此更改不应影响您发布/保存中的任何内容。另请注意>您正在告诉 AJAX 发布,但您正在检查 $_GET 全局变量。您将需要使用 $_POST['ecovauserName'] 和 $_POST['ecovauserage']
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      您似乎使用跨域进行查询,而 jsonp 使这成为可能。

      所以,在你的 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) . ')';
      

      在你的 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');
          }
      });
      

      更多信息http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp

      【讨论】:

      • 感谢您的回复,我们是 PHP 新手,请说明我们在哪里放置了两行 header('content-type: application/json; charset=utf-8');回声 $_GET['callback'] 。 '(' . json_encode($response) . ')';
      • 我们得到错误:function() { console.log('error'); } 错误
      • 添加调试error: function (jqXHR, textStatus, errorThrown) { console.log(jqXHR); console.log(textStatus); console.log(errorThrown); }
      • 我们得到了错误这个对象解析器错误错误:jQuery111002985340391751379_1425647499106 was not called file:///D:/SaveApp/SaveApp/assets/ForLL/jquery.mobile-1.4.2.min.map 加载失败资源:net::ERR_FILE_NOT_FOUND
      猜你喜欢
      • 2016-12-16
      • 2019-04-22
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 2021-01-06
      • 2016-11-09
      • 2011-02-22
      相关资源
      最近更新 更多