json没有值,但print_r有值php pdo jquery ajax

时间:2015-01-20 05:12:20

标签: php jquery ajax json pdo

if (isset($_POST['continentid'])) {
      $stmt = $dbh->prepare("SELECT * FROM country_tbl WHERE parent_id = ? ");
      $stmt->bindValue(1, $_POST['continentid'], PDO::PARAM_STR);
    if ($stmt->execute()) {
        if ($stmt->rowCount() > 0) {
            while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $country[] = array('sysid' => $selected_row['sys_id'],'name' => $selected_row['countryname']);
            } 
            //print_r($country);
            echo json_encode($country);
            //echo "312321321321";
            //return $country;

        }
    }
}


$.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
       for(var i = 0; i < data.length; i++) {
           console.log("PAIR " + i + ": " + data[i].sysid);
           console.log("PAIR " + i + ": " + data[i].name);
       }
    }
});

我上面的代码使用jquery ajax发送请求它将返回一个id,它将用作select语句的参数。然后我在选择选项框中使用这些值。我已为此here发布了一个问题。它工作正常现在奇怪的是大陆上的第一个值没有给出国家列表如果使用json但是当我使用print_r它给我国家列表但是对于其他大陆json值是好的我有价值对于国家。问题是为什么列表中的第一个值不给出json值,但是如果print_r它有值,这个设置有什么问题?

Update

如果我这样做

的print_r($国家); echo json_encode($ country);

表示第一个元素

Array
(
    [0] => Array
        (
            [sysid] => 1
            [code] => 140101000
            [name] => China
            [parentid] => 1
        )

    [1] => Array
        (
            [sysid] => 2
            [code] => 140102000
            [name] => Japan
            [parentid] => 1
        )

    [2] => Array
        (
            [sysid] => 3
            [code] => 140103000
            [name] => Hongkong
            [parentid] => 1
        )
)

如果我这样做

的print_r($国家); echo json_encode($ country);

表示第二个元素

Array
(
    [0] => Array
        (
            [sysid] => 1
            [code] => 140101000
            [name] => China
            [parentid] => 1
        )

    [1] => Array
        (
            [sysid] => 2
            [code] => 140102000
            [name] => Japan
            [parentid] => 1
        )

    [2] => Array
        (
            [sysid] => 3
            [code] => 140103000
            [name] => Hongkong
            [parentid] => 1
        )
)


[
{"sysid":"1","code":"140101000","name":"China","parentid":"1"},{"sysid":"2","code":"140102000","name":"Japan","parentid":"1"},{"sysid":"3","code":"140103000","name":"Hongkong","parentid":"1"}
]

UPDATE

我想我找到了问题,虽然我还没有找到解决方案,但我认为字符ñ'是他们不会为json返回任何关于如何让它返回的原因的原因这些价值观?

4 个答案:

答案 0 :(得分:0)

试试吧。 JSON.parse(data)将字符串转换为json。

    $.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
  data=  JSON.parse(data);
      data.each(key,value)  {

           console.log("PAIR " + i + ": " + data[key].sysid);
           console.log("PAIR " + i + ": " + data[key].name);
       }
    }
});

答案 1 :(得分:0)

试试这个,

success: function(data) {
   $(data).each(function(i,v){
       console.log("PAIR " + i + ": " + v.sysid);
       console.log("PAIR " + i + ": " + v.name);
   });
}

&#13;
&#13;
var data = [{
  "sysid": "1",
  "code": "140101000",
  "name": "China",
  "parentid": "1"
}, {
  "sysid": "2",
  "code": "140102000",
  "name": "Japan",
  "parentid": "1"
}, {
  "sysid": "3",
  "code": "140103000",
  "name": "Hongkong",
  "parentid": "1"
}];
$(data).each(function(i, v) {
  console.log("PAIR " + i + ": " + v.sysid);
  console.log("PAIR " + i + ": " + v.name);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

更改一段php代码,如

if ($stmt->rowCount() > 0) {
     $country=array();// initialise $country first
     while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $country[] = array('sysid' => $selected_row['sys_id'],'name' => $selected_row['countryname']);
     } 
     echo json_encode($country);
 }

答案 2 :(得分:0)

$.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
  data=  JSON.parse(data);
      data.each(key,value)  {

           console.log("PAIR " + i + ": " + value.sysid);
           console.log("PAIR " + i + ": " + value.name);
       }
    }
});

答案 3 :(得分:0)

将此行$dbh->query("SET CHARACTER SET utf8");添加到我的配置文件中修复了问题所以它确实与ñ json_encode等特殊字符有关,如果有特殊字符则返回null值