weixin_33696822 2015-10-09 03:55 采纳率: 0%
浏览 12

json编码值问题

I know this is basic but i don't know what else i can do. So i need your help. Ill tell you what happened.

The story is that i have a database request and I want to json encode the data from that request and use it through a javascript file ($.ajax or $.json).

the data i need from that request are two values. Latitude and longitude. but when i receive the json_encode from php and put it in the console log or an alert it displays undefined instead of the "double precision"(because the json string).

Ill show you the code.

the php

    <?php
$dbconn3 = pg_connect
    ("host= localhost port=5432 dbname=EmergenciesResponse    user=postgres password=asdf");                


    $query1=pg_query
    ("select latitude, longitude from emergency where   id_emergency=5");

    $arreglo=array();
              while($reg=pg_fetch_assoc($query1)){
               $arreglo[]=$reg;

             }

              echo json_encode($arreglo);  

             pg_close($dbconn3);

              ?>

Then the .js code

 $.ajax({                                      
          url: 'consulta2.php',                  
          data:{
                },                        
          method: 'POST',                               
          dataType:'json',                
          success: function(data)          
          {                   
              alert("latitude:"+data.latitude+"longitude:"+data.longitude);        


          }
        });

As I said when I do an alert instruction the output prints undefined but the string of the php is correct. Sorry if its very easy but I don't know what to do about the undefined value

thx for your time.

  • 写回答

2条回答 默认 最新

  • weixin_33725722 2015-10-09 04:00
    关注

    Your PHP returns an array of objects. The response will look like this (without pretty print):

    [
        {
            "latitude": 0.000,
            "longitude": 0.000
        },
        {
            "latitude": 1.000,
            "longitude": 2.000
        },
        {
            "latitude": 3.000,
            "longitude": 4.000
        }
    ]
    

    However, in JS you are treating is as an object.

    You would understand what the problem is if you simply opened your consulta2.php file and looked at the response.

    There can be two causes:

    1. id_emergency is not unique. Then, your PHP is good, but in JS you need to iterate through the array:

      $.ajax({                                      
          url: 'consulta2.php',                  
          data:{
          },                        
          method: 'POST',                               
          dataType:'json',                
          success: function(data)          
          {  
              $.each(data, function() { // <-------
                      alert("latitude:"+this.latitude+"longitude:"+this.longitude);
              });
          }
      });
      

      You will get from 0 to many alerts, one for every pair of coordinates.

    2. id_emergency is unique. Then, your JS is good, but in PHP you do not need to form an array. It should be as simple as:

      $query1=pg_query("select latitude, longitude from emergency where   id_emergency=5");
      
      //$arreglo=array(); // don't need an array
      
      $reg = pg_fetch_assoc($query1); // only 1 object
      echo json_encode($reg); // encode the object, but not an array
      
      pg_close($dbconn3);    
      

      As this is the single object, you will get only one alert.

    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序