dongxie9448 2016-06-27 13:51
浏览 46

php ldap json用jquery返回

I've a problem with a returned json value, when the ldap result is valid the json return is OK but when the result is invalid the json is invalid too. I'm using uwamp. I'm a newbie.

Thank you all.

php code :

<?php
if (is_ajax()) {
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $_POST["action"];
    switch($action) { //Switch case for value of action
      case "test": test_function(); break;
    }
  }
}
//Function to check if the request is an AJAX request
function is_ajax() {
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function test_function(){
  $return = $_POST;
  $return["code"] = "NO PASSWORD";
  //$ldapconn = FALSE;

  $return["username"] = 'intra\\'.$return["username"];
  ($return["password"]) ? $ldapconn = ldap_connect("ldap://xxx:389") : $ldapconn = FALSE ;
  if ($ldapconn) {

      $ldapbind = ldap_bind($ldapconn, $return["username"], $return["password"]);

      $return["code"] = ($ldapbind) ? "VU" : "VI" ;
      $ldapunbind = ldap_unbind($ldapconn);

  }
  $return["json"] = json_encode($return);
  echo json_encode($return);

}
?>

html code :

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="https://code.jquery.com/jquery-3.0.0.js"></script>
  <script type="text/javascript">
  $("document").ready(function(){
    $(".js-ajax-php-json").submit(function(event){
      var data = {
        "action": "test"
      };
      data = $(this).serialize() + "&" + $.param(data);
      $.ajax({
        type: "POST",
        dataType: "json",
        url: "ldap.php", //Relative or absolute path to response.php file
        data: data,
        success: function(data) {
          $(".the-return").html(
            "username: "         + data["username"] +
            "<br />password: "   + data["password"] +
            "<br />Code: "       + data["code"] +
            "<br />JSON: "       + data["json"]
          );
          alert("Form submitted successfully.
Returned json: " + data["json"]);
        },
        error: function (request, status, error) {
          alert(request.responseText + "-" + error + "-" + request);
        }
      });
      //return false;
      event.preventDefault();
    });
  });
  </script>
  <title>Document</title>
</head>
<body>
  <form action="" class="js-ajax-php-json" method="post" accept-charset="utf-8">
    <input type="text"     name="username" value="" placeholder="username" />
    <input type="password" name="password" value="" placeholder="password" />

    <input type="submit" name="submit" value="Submit form"  />
  </form>

  <div class="the-return">
    [HTML is replaced when successful.]
  </div>
</body>
</html>

error code :


( ! ) Warning: ldap_bind(): Unable to bind to server: Invalid credentials in E:\SOFT\WebServer\UwAmp\www\json_validation\ldap.php on line 24 Call Stack #TimeMemoryFunctionLocation 10.0340138696{main}( )...\ldap.php:0 20.0340138800test_function( )...\ldap.php:6 30.0340139424http://www.php.net/function.ldap-bind' target='_new'>ldap_bind ( )...\ldap.php:24 {"username":"intra\username","password":"qsdqsdqs","action":"test","code":"VI","json":"{\"username\":\"\\xxx\",\"password\":\"qsdqsdqs\",\"action\":\"test\",\"code\":\"VI\"}"}-SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data-[object Object]

  • 写回答

2条回答 默认 最新

  • douzou7012 2016-06-27 13:55
    关注

    You'll need to handle the warning.

    Best way to do, is to convert it into an exception, and catching the exception with a try/catch.
    That way you won't have output, and you can have a correct JSON string.

    Snippet example for converting warnings to Exceptions:

    set_error_handler(
                function (
                    $errno,
                    $errstr,
                    $errfile,
                    $errline,
                    array $errcontext
                ) {
                    // error was suppressed with the @-operator
                    if (0 === error_reporting()) {
                        return false;
                    }
    
                    throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
                }
            );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大