【问题标题】:Not being able to use Json data from php page in jquery无法在jquery中使用来自php页面的Json数据
【发布时间】:2016-01-28 14:35:30
【问题描述】:

我正在尝试使用 jquery 解析从 php 页面发送的 json 数据,但我得到的结果是:{

我不知道如何解决它, 我在 php 中创建 json 数据,如下代码:

$load_op_cm = $DBM -> RunQuery("SELECT * FROM at_ops_cmnts WHERE op_id='$op_id'",true,false);
    $row = mysqli_fetch_assoc($load_op_cm);
    $j_result = json_encode($row);
    echo $j_result;

我正在尝试使用 jquery 解析这些数据,我正在使用 jquery post 方法发送数据并获得响应,并且我正在尝试获取这部分数据对我有用:@987654326 @ 不仅是这个,还有所有 cm_ 变量及其值。

当我以 html 格式显示响应时,它就像:

这是我的 jquery 代码:

 $(function(){
            var vop_id = localStorage.getItem('op_id');
            $.post("Requests/OPS.php", //Required URL of the page on server
                {   // Data Sending With Request To Server
                    Load_OP_CM : true,
                    op_id : vop_id
                },
                function(response){  // Required Callback Function
                    var result = response;
                    $.each(result, function (i,elem) {
                        $("#Response").text(elem);
                    });

                });

关于如何获取这些数据有什么建议吗?

【问题讨论】:

    标签: php jquery html json parsing


    【解决方案1】:

    首先,您需要解析 JSON 字符串 var result = JSON.parse(response);。 然后,当您遍历时,您需要过滤结果以仅包含包含“cm_”的键

    var filter = "cm_";
    $.each(result, function (i,elem) {
        if(i.indexOf(filter) !== -1) {
            console.log(i, elem);
            // Or whatever you need to do…
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2016-02-04
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多