【问题标题】:cannot receive json from server through php无法通过php从服务器接收json
【发布时间】:2016-02-26 05:19:02
【问题描述】:

我无法从服务器上的 php 获取 json

javascript 代码是:

        $.ajax({
            type: "POST",
            url: "doingSQL.php",
            data: label,
            success: function(result) {
                $("#p").html("All my book: <br>"+ result);
                console.log(result);
                },
            dataType: "json",
            error: function(xhr){
                console.log("error");
               }
      });

doingSQL.php 的工作是从 SQL 数据库中选择 bookName 并将数据转换为 json。它看起来像这样:

   /* the server connecting code is omitted */

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $label = $_POST["label"];
    }
            $sql = "SELECT * FROM book WHERE ower = '". $label."'";
            $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0) {

            // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                $Arr = array("id" => $row["book_id"],
                       "bookName" => $row["bookName"]);

                $bookDetail[] = array( "book".$i => $Arr);

        }}


        }

    mysqli_close($conn);
    $json = array("mybook" => $bookDetail);
    echo  json_encode($json);//  return json 

但我在 html 控制台中得到的结果是“[]”或数组 [0]。

json是合法的json格式,如下:

{  
   "mybook":[  
      {  
         "book0":{  
            "id":"0",
            "bookName":"bookA"
         }
      },
      {  
         "book1":{  
            "id":"1",
            "bookName":"bookB"
         }
      }
   ]
}

但是,如果代码在 php 中的 SQL 连接之外json 返回将成功。 它看起来像:

/* the server connecting code is omitted */

mysqli_close($conn);
// if outside the SQL connection

$ArrA = array("id" => "0", "bookName" => "bookA");
$ArrB = array("id" => "1", "bookName" => "bookB");

$bookDetail[] = array( "book0" => $ArrA);
$bookDetail[] = array( "book0" => $ArrB);

$json = array("mybook" => $bookDetail);
echo  json_encode($json);// return json success

有什么想法吗?

【问题讨论】:

  • 关闭连接后,您似乎正在分配从查询返回的值。
  • 另请注意,如果这是生产代码,您很容易受到SQL Injection

标签: javascript php json ajax post


【解决方案1】:

只需将您的 ajax data 传递为:

data: {label:label}

【讨论】:

    【解决方案2】:

    ajax settingsdata 属性的类型可以是 PlainObjectStringArray。如需更多参考,请参阅此http://api.jquery.com/jquery.ajax

    所以你的 javascript 代码会是这样的:

    $.ajax({
      type: "POST",
      url: "doingSQL.php",
      data: {label: label},
      success: function(result) {
        $("#p").html("All my book: <br>"+ result);
        console.log(result);
      },
      dataType: "json",
      error: function(xhr){
        console.log("error");
      }
    });
    

    【讨论】:

    • 为什么我的php在编辑数据前仍然可以获取标签值:{label: label}?比如我可以在php中使用label值做sql选择,但就是无法返回JavaScript。
    【解决方案3】:

    您需要在变量中传递label 值。现在因为在 PHP 页面上您使用的是$_POST['label'],所以像这样传递变量:

    data: {label: label},
    

    所以你完整的 ajax 代码应该是这样的:

    $.ajax({
      type: "POST",
      url: "doingSQL.php",
      data: {label: label}, // changed here
      success: function(result) {
        $("#p").html("All my book: <br>"+ result);
        console.log(result);
      },
      dataType: "json",
      error: function(xhr){
        console.log("error");
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2018-10-10
      • 1970-01-01
      • 2019-10-19
      相关资源
      最近更新 更多