dongshushi5579 2016-08-14 18:21
浏览 82
已采纳

无法在javascript中读取Json_encode的键值对

I am having issues with reading Json_encode response in a java script.

The PHP file reads values from database and sends the results as Json_encode array to html.

<?php  
 include("connect.php"); 
 try {

    $conn = new PDO("mysql:host=$servername;dbname=mydb", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "Call print_awb (@output1,@output2,:input_awb_ref_id)";
    if (isset($_POST)) {
        $p_in_awb_ref_id =reset($_POST["var_p_in_awb_ref_id"]);
    }
    $stmt = $conn->prepare($sql);               
    $stmt->bindParam(':input_awb_ref_id',$p_in_awb_ref_id, PDO::PARAM_INT);
    $stmt->execute();
    $out_awb_ref_id  = $conn->query("SELECT @output1")->fetch(PDO::FETCH_ASSOC); 
    $out_agent_id  = $conn->query("SELECT @output2")->fetch(PDO::FETCH_ASSOC); 

    $output = array(            
        "out_awb_ref_id" => $out_awb_ref_id,
        "out_agent_id" => $out_agent_id,
    );
    echo json_encode($output);
    $stmt->closeCursor(); 
}   
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
?>

The HTML CODE

<script type="text/javascript">

        function get_parameters(){

            var quote = ABCD1234;
            quote.toString();
            window.alert(quote);
            $.ajax({
                type:  "POST",
                url: "printawb.php",
                data: {var_p_in_awb_ref_id:quote},
                dataType: "text",
                success: function (result) {

                    alert(result);
                    var a = result.out_awb_ref_id;
                    alert (a);
                    alert(result['out_awb_ref_id']);
                    alert(result['out_agent_id'])
                },
                error:function (jqXHR, status, err){
                    //Fail
                    layer_1.html(html);
                }

            });
            return vars;

        };
</script>

The response I have is

HVP000062

{"out_awb_ref_id":{"@output1":"MIR"},"out_agent_id":{"@output2":"rtPreston"}} undefined undefined

All Need is the values in the 2nd line of the result ie. "MIR" and "rtPreston"

I have tried couple of things: 1. changed the calling function type as 'JSON' but the response was 'Object' without values 2. Tried converting to jsonString 3. tried reading values using JSON.parse(jsonString); 4. result['out_ref_awb_id']

None of them work. Can someone help me how I can get values of these? I can then use them to populate on a html page.

Many thanks

  • 写回答

1条回答 默认 最新

  • duanhua9398 2016-08-15 08:57
    关注

    Add this as a first line in the HEAD section of your HTML template

    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    

    Then try this code:

    <script type="text/javascript">
    
        function get_parameters(){
    
            var quote = ABCD1234;
            quote.toString();
            window.alert(quote);
            $.ajax({
                type:  "POST",
                url: "printawb.php",
                data: {var_p_in_awb_ref_id:quote},
                dataType: "text",
                success: function (result) {
    
                    var data = jQuery.parseJSON(result);
                    var param1 = data.out_agent_id;
                    var param2 = data.out_awb_ref_id;
                    alert(param1["@output2"]);
                    alert(param2["@output1"])
                },
                error:function (jqXHR, status, err){
                    //Fail
                    layer_1.html(html);
                }
            });
            return vars;
    
        };
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?
  • ¥50 复现论文;matlab代码编写