[英]unable to parse json using jquery after ajax call
this is my php script 这是我的PHP脚本
<?php
$wishes_array["wishes_text"] = null;
$wishes_array["total"] = null;
$count = 0;
// wishes has all records
if(!is_null($wishes))
{
foreach($wishes as $wish)
{
if($wish->text != null)
{
$wishes_array["wishes_text"][$count] = html_escape($wish->text);
$count++;
}
}
$wishes_array["total"] = sizeof($wishes);
echo json_encode($wishes_array);
}
?>
background 背景
a user sends his wishes using a button, but he has an option to send some message with his wish, suppose person A just sent wish, but person B sent wish with text "happy birthday", person C sent wish with text "have a great birthday" 用户使用按钮发送其愿望,但他可以选择发送带有其愿望的消息,假设某人A刚刚发送了愿望,但人B发送了带有“生日快乐”文本的愿望,人C发送了带有文本“具有a愿望”的愿望生日快乐
now using this array we have array which tells total
number of wishes recieved(with and without wish text) and wishes
the text of the wishes sent by person. 现在使用此数组,我们得到一个数组,该数组告诉接收的愿望
total
(带有或不带有愿望文本),并wishes
由人发送的愿望文本。
problem 问题
unable to use parse the json using jquery, here is what i've done and it return undefined
无法使用jquery解析json,这是我所做的,并且返回
undefined
var total_start = '<div style="padding: 3px;margin: 10px auto;"><i class="fa fa-heart" style="background: #e53935; color: #fff;padding: 5px;border-radius: 50%;"></i> ';
var total_end = '</div>';
var startString = '<div style="padding: 3px;margin: 10px auto;"><i class="fa fa-heart" style="background: #e53935; color: #fff;padding: 5px;border-radius: 50%;"></i>';
var endSting = '</div>';
var wishes = '';
var count = 0;
if(data["wishes_text"] != null){
$.each(data, function(index, element) {
wishes += startString;
wishes += ' ' + data["wishes_text"][count];
wishes += endSting;
count++;
});
}
var total = "";
if(data["total"] == 1)
{
total = total_start + data["total"] + ' person wished' + total_end;
}
else
{
total = total_start + data["total"] + ' people wished' + total_end;
}
$('#wish_div').html(total + wishes);
ajax response json Ajax响应JSON
{"wishes_text":["wish 1","wish 2"],"total":3}
The issue in your code is simply that you're attempting to iterate through data
instead of the wishes_text
array contained within data
. 在你的代码的问题很简单,你试图通过遍历
data
,而不是wishes_text
包含在阵列data
。 Note that you can also tidy up the logic slightly. 请注意,您也可以稍微整理一下逻辑。 Try this:
尝试这个:
var total = '<div style="padding: 3px;margin: 10px auto;"><i class="fa fa-heart" style="background: #e53935; color: #fff;padding: 5px;border-radius: 50%;"></i>' + data["total"] + ' ' + (data.total == 1 ? 'person' : 'people') + ' wished</div>';
var startString = '<div style="padding: 3px;margin: 10px auto;"><i class="fa fa-heart" style="background: #e53935; color: #fff;padding: 5px;border-radius: 50%;"></i>';
var endSting = '</div>';
var wishes = '';
if (data.wishes_text != null) {
$.each(data.wishes_text, function(index, element) {
wishes += startString + ' ' + data["wishes_text"][index] + endSting;
});
}
$('#wish_div').html(total + wishes);
I'd also suggest you look in to using stylesheets and classes over inline styling, and also a templating library as the large chunks of HTML in the JS code are on the limit of being 'too big', from a best practice point of view. 我还建议您考虑使用内联样式中的样式表和类,以及模板库,因为从最佳实践的角度来看,JS代码中的HTML很大块的限制是“太大” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.