我试图用ajax在两个div上显示两组数据。
我使用以下代码执行此操作。
这是ajax代码
$(function () {
$(".myBtn").click(function () {
var id = $(this).data("id");
var name = $(this).data("name");
var dataString = 'id=' + id + '&list_id=' + <? php echo $id; ?> ;
var parent = $(this);
if (name == 'up') {
$(this).fadeIn(200).html;
$.ajax({
type: "POST",
url: "vote_up.php",
data: dataString,
cache: false,
dataType: 'json',
success: function (json) {
parent.parent().find(".v-up").html(json.msg1);
parent.parent().find(".v-down").html(json.msg2);
}
});
} else {
$(this).fadeIn(200).html;
$.ajax({
type: "POST",
url: "vote_down.php",
data: dataString,
cache: false,
dataType: 'json',
success: function (json) {
parent.parent().find(".v-down").html(json.msg2);
parent.parent().find(".v-up").html(json.msg1);
}
});
}
return false;
});
});
这是杰森代码
echo json_encode(array('msg1'=>$value1));
return;
echo json_encode(array('msg1'=>$value2));
return;
代码确实似乎有效。它显示了msg1,但它没有显示msg2
有人可以指出我如何做到这一点或我在这里做错了什么?
答案 0 :(得分:3)
从您提供的最少量的PHP看来,您希望这样做
echo json_encode(array('msg1'=>$value1, 'msg2'=>$value2));
return;
这意味着当您尝试访问值时,就像您有
一样success: function (json) {
parent.parent().find(".v-down").html(json.msg2);
parent.parent().find(".v-up").html(json.msg1);
}
他们会在那里
答案 1 :(得分:1)
尝试将两个json字符串组合成一个并在脚本中将它们拆分。 另外你应该看一下,没有什么比json-String更多了。 例如像这样: // PHP
$array = array("msg1"=>$value1, "msg2"=>$value2);
$toJSON = json_encode($array);
echo $toJSON;
die(); // To prevent more output after this json-String
// JS
[...]
success: function (json) {
parent.parent().find(".v-down").html(json.msg2);
parent.parent().find(".v-up").html(json.msg1);
}
[...]