Ajax Json 不允许双引号结果
我在这段代码上遇到了问题。 json 不允许双引号结果。前任。如果我有一个结果(一个悲伤的“故事”),json 不会解析它。
$('.loader1').show();
$.ajax({
url: "findBook.php?action=populatemyacademy&bookCatId="+parentCategoryId+"",
cache: false,
dataType: "json",
success: function(data) {
$('.loader1').hide();
$.each(data.items, function(i,item){
$("#myAcademy").append('<div class="nameWrapper"><input name="checkMeM" type="checkbox" value="'+item.bookId+'"/><span title="'+item.bookName+'" class="checkBoxSpan">'+item.bookName+'</span></div>');
});
}});
这是我在 php 脚本上生成 json 的方法
$sql = "SELECT academyBookId, title FROM academy_book WHERE academyBookCategoryId = $bookCatId AND academyId = $academyId";
$qPopulateAcademy = mysql_query($sql, $dbConMain) or die(__LINE__.' '.$sql.' '.mysql_error());
while($rPopulateAcademy = mysql_fetch_array($qPopulateAcademy)){
$rows[] = array('bookId' => $rPopulateAcademy [0], 'bookName' => $rPopulateAcademy [1]);
}
$jsdecode = json_encode($rows);
echo "{items:$jsdecode}";
I got a problem on this code. json wont allow double quote results. ex. if i have a result (A sad "Story") json wont parse it.
$('.loader1').show();
$.ajax({
url: "findBook.php?action=populatemyacademy&bookCatId="+parentCategoryId+"",
cache: false,
dataType: "json",
success: function(data) {
$('.loader1').hide();
$.each(data.items, function(i,item){
$("#myAcademy").append('<div class="nameWrapper"><input name="checkMeM" type="checkbox" value="'+item.bookId+'"/><span title="'+item.bookName+'" class="checkBoxSpan">'+item.bookName+'</span></div>');
});
}});
Here's how i generate json on my php script
$sql = "SELECT academyBookId, title FROM academy_book WHERE academyBookCategoryId = $bookCatId AND academyId = $academyId";
$qPopulateAcademy = mysql_query($sql, $dbConMain) or die(__LINE__.' '.$sql.' '.mysql_error());
while($rPopulateAcademy = mysql_fetch_array($qPopulateAcademy)){
$rows[] = array('bookId' => $rPopulateAcademy [0], 'bookName' => $rPopulateAcademy [1]);
}
$jsdecode = json_encode($rows);
echo "{items:$jsdecode}";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是服务器在 JSON 文档中将其呈现为 " ,但这不起作用,因为 json_encode 使用 "" 进行换行,因此您可以执行以下操作:
这将用十六进制代码替换撇号和引号。
The problem is the server renders it as " in your JSON document and this won't work work because json_encode uses "" to wrap, so you can do the following:
which will replace the apostrophes and quotes with their hex codes.