json 不是值,但 print_r 有值 php pdo jquery ajax
json not value but print_r has value php pdo jquery ajax
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 发送请求,它将 return 一个 id 用作 select 语句的参数。然后我在 select 选项框中使用这些值。我已经针对此 发布了一个问题。它现在工作正常,奇怪的是如果使用 json 大陆上的第一个值没有给出国家列表但是当我使用 print_r 时它给了我国家列表但是对于另一个大陆json 价值还可以,我正在为国家/地区获取价值。问题是为什么列表中的第一个值没有给出 json 值,但如果 print_r 它有值,这个设置有什么问题?
Update
如果我这样做
print_r($国家);
echo json_encode($国家);
第一个元素
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($国家);
第二个元素
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
我想我找到了问题,虽然我还没有找到解决方案,但我认为字符 ñ
和 '
是他们不会 return 值 [=51] 的原因=] 关于如何实现 return 这些值的任何想法?
试一试。 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);
}
}
});
试试这个,
success: function(data) {
$(data).each(function(i,v){
console.log("PAIR " + i + ": " + v.sysid);
console.log("PAIR " + i + ": " + v.name);
});
}
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);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
将 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);
}
$.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);
}
}
});
将这一行 $dbh->query("SET CHARACTER SET utf8");
添加到我的配置文件中解决了这个问题,因此它确实与特殊字符有关,例如 ñ
json_encode 和 return 空值如果有特殊字符
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 发送请求,它将 return 一个 id 用作 select 语句的参数。然后我在 select 选项框中使用这些值。我已经针对此
Update
如果我这样做
print_r($国家); echo json_encode($国家);
第一个元素
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($国家);
第二个元素
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
我想我找到了问题,虽然我还没有找到解决方案,但我认为字符 ñ
和 '
是他们不会 return 值 [=51] 的原因=] 关于如何实现 return 这些值的任何想法?
试一试。 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);
}
}
});
试试这个,
success: function(data) {
$(data).each(function(i,v){
console.log("PAIR " + i + ": " + v.sysid);
console.log("PAIR " + i + ": " + v.name);
});
}
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);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
将 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);
}
$.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);
}
}
});
将这一行 $dbh->query("SET CHARACTER SET utf8");
添加到我的配置文件中解决了这个问题,因此它确实与特殊字符有关,例如 ñ
json_encode 和 return 空值如果有特殊字符