服务器回显字母,而不是整个单词 [英] server echoing letters rather than whole words

查看:90
本文介绍了服务器回显字母,而不是整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下内容将数据库中的数据返回到我的视图.数据库列包含以下数据:

I'm using the following to get data from a database back to my view. The database column has the following data:

'hello'
'howdy'
'blablabl'

但是,当我使用下面的php并在jQuery ajax成功调用中对其进行检索时,它会以字符串形式获取文字数组,因此当我执行以下操作时:

however, when I use the php below, and retrieve it inside jQuery ajax success call, its getting a literal array as a string, so when I do the following:

success: function(data){
for (var i = 0; i < data.length; i++) {
    var comment = data[i];

data[i]'[', '"', 'h', 'e','l','l'等...如何避免这种情况

data[i] is '[', '"', 'h', 'e','l','l' etc... how can I avoid this

    if ($result = $mysqli->query("SELECT * FROM comments")) {
        $row_cnt = $result->num_rows;
        if ($row_cnt > 0) {
            while($row = $result->fetch_assoc()) {
                 $array[] = $row["comment"];
            }
            echo json_encode($array);
        } else {
            echo "no data";
        }

        $result->close();
    }

推荐答案

由于要从php传递JSON字符串,因此需要首先将其解析为javascript中的对象.

Since you are passing a JSON string from the php, you need to first parse it into an object in your javascript.

您可以按如下所示进行操作-

You can do this as shown -

obj = JSON.parse(data);

然后您可以从obj获取您的值.

You can then get your value from the obj.

这篇关于服务器回显字母,而不是整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆