javascript - jquery: Can't read the response from php function -
i'm trying read response php function response undefined value. if use chromes inspect element , go in network tab can read correct value of item.
here code:
javascript function
function chatheartbeat(){ $.ajax({ url: 'chat.php?action=chatheartbeat', type: 'post', datatype: 'json', success: function(response) { $.each(response.items, function(item){ //alert(item.m); $(".chatboxcontent").append('<div class=chatboxmessage><span class="chatboxmessagefrom">'+item.f+': </span><span class="chatboxmessagecontent">'+item.m+'</span></div>'); }); settimeout('chatheartbeat();',3000); }, error: function(){ $(".chatboxcontent").append('<div class=chatboxmessage><span class="chatboxmessagefrom">error!</span><span class="chatboxmessagecontent">unknown</span></div>'); alert("failed"); } }); }
here php code:
function chatheartbeat() { if ($idchat!=""){ $sql = "select * chat idchat=".$idchat." , recd=0 order id asc"; $result=$mysqli->query($sql); if ($result->num_rows>0){ while($row=$result->fetch_array()){ $sqlup="update chat set recd = '1' idchat = ".$row['id']; if ($mysqli->query($sqlup)=== false){ echo "error updating record" . $mysqli->error; } $item.=<<<eod { "f": "{$row['from']}", "m": "{$row['message']}" }, eod; } } }else{ $item.=$item.=<<<eod { "f": "{$mysqli->connect_errno}", "m": "{$mysqli->connect_error}" }, eod; } } }else{ $item.=$item.=<<<eod { "f": "{$mysqli->connect_errno}", "m": "{$mysqli->connect_error}" }, eod; } ?> { "items": [ <?php echo $item;?> [ } <?php } ?>
this might happen if respond malformed json format. try putting data in array/object , use json_encode on it:
<?php $data = []; $data["f"] = "value1"; $data["m"] = "value2"; echo json_encode($data); ?>
Comments
Post a Comment