doq13207 2013-07-26 06:18
浏览 12
已采纳

返回JSON,用Javascript编写

Hy all!

I have a little problem. I tried to write an ajax post to get values from a database, and it's returning format is an a JSON object. How can I get the key, and value pairs from it?

the jquery which sending the ajax:

function getTableData() {

        $.post('loader.php',getGetStr(),function(data){
                        var json = $.parseJSON(data);
                        console.log(json);
        });
    }

The console.log output is:

Object {query-data: Array[3]}
query-data: Array[3]
0: Object
buy_type: "kiado"
condition_type: "uj"
district: "1"
heat_type: "cirko"
id: "1"
lift_type: "all"
parking_type: "all"
price_max: "22"
price_min: "10"
prop_type: "lakas"
room_max: "3"
room_min: "1"
street: "all"
uid: "3"
__proto__: Object
1: Object
2: Object
length: 3
__proto__: Array[0]
__proto__: Object

The php code wich select the rights things, and return the data to the ajax:

$ordering = array ("buy_type " . $_POST['buyType'],"prop_type     ".$_POST['propertyType'],"district ".$_POST['disctrict'],
                    "street ".$_POST['street'],"room_min     ".$_POST['roomMin'],"room_max ".$_POST['roomMax'],
                    "price_min ".$_POST['priceMin'],"price_max     ".$_POST['priceMax'],"condition_type ".$_POST['conditionType'],"heat_type ".$_POST['heatType'],"lift_type ".$_POST['liftType'],"parking_type ".$_POST['parkingType']);

$user=$_SESSION["user"];
$whois = $mysqli->query('SELECT * FROM users WHERE uid='.$mysqli->real_escape_string($user).' ');
$who = $whois->fetch_assoc();

switch($who['user_title']){
    case '0':
        $res=$mysqli->query('SELECT * FROM searches WHERE uid='.$mysqli->real_escape_string($user).' 
                ORDER BY '.$mysqli->real_escape_string($ordering[0]).',
                        '.$mysqli->real_escape_string($ordering[1]).',
                        '.$mysqli->real_escape_string($ordering[2]).',
                        '.$mysqli->real_escape_string($ordering[3]).',
                        '.$mysqli->real_escape_string($ordering[4]).',
                        '.$mysqli->real_escape_string($ordering[5]).',
                        '.$mysqli->real_escape_string($ordering[6]).',
                        '.$mysqli->real_escape_string($ordering[7]).',
                        '.$mysqli->real_escape_string($ordering[8]).',
                        '.$mysqli->real_escape_string($ordering[9]).',
                        '.$mysqli->real_escape_string($ordering[10]).',
                        '.$mysqli->real_escape_string($ordering[11]).'
                  ') or die($mysqli->error);
        while($ki=$res->fetch_assoc()){
            $tomb[] = $ki;
        }
        $tomb = array("query-data"=>$tomb);
        echo json_encode($tomb);

can anybody help to me to write these values to a table?

  • 写回答

4条回答 默认 最新

  • dongsaohu6429 2013-07-26 06:36
    关注

    You can do something like this.

    UPDATED:

    $.post( 'loader.php', getGetStr(), function( data ) {
      if ( !data || !data['query-data'] ) {
        // invalid json string, so dont process
        return;
      }
      data  = data['query-data'];
      // create the table
      var table = $("<table />").html('<thead></thead><tbody></tbody>');
      // inserted table head cols?
      var thead = false;
    
      // loop through 'query-data'
      for( i = 0; i < data.length; i++ ) {
        // append 'tr' element to 'tbody'
        var tr1 = $("<tr />").appendTo( table.find("tbody") );
        if ( !thead ) {
          // if not finished creating table head cols, then append 'tr' elemnts to thead
          var tr2 = $("<tr />").appendTo( table.find("thead") );
        }
        // loop if its an object
        if ( typeof data[i] === "object" ) {
          for( j in data[i] ) {
            if ( !thead ) {
              // if not finished creating table head cols, then append 'th' elements to thead
              $("<th />").html( j ).appendTo( tr2 );
            }
            // insert our real dat to table rows
            $("<td />").html( data[i][j] ).appendTo( tr1 );
          }
          // we finished creating table head cols
          thead = true;
        }
      }
    
      // append the table to whatever element you want,
      // you can also use $("body").html( table );
      table.appendTo( $("body") );
    }, "json" );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序