php - 返回JSON,用Javascript编写

标签 php javascript ajax json

大家好!

我有一个小问题。我尝试编写 ajax 帖子来从数据库获取值,它的返回格式是 JSON 对象。 如何从中获取键和值对?

发送ajax的jquery:

函数 getTableData() {

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

console.log 输出为:

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

选择权限的php代码,并将数据返回给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);

有人可以帮我将这些值写入表格吗?

最佳答案

你可以做这样的事情。

更新:

$.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" );

关于php - 返回JSON,用Javascript编写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17874254/

相关文章:

php - PHP/MySQL 中的间歇性 "No Database Selected"?

javascript - 无法使用 Vanillabox 正确显示图像

c# - 动态设置 opentok 中 token 的 EXPIRE_TIME 从 .net 中的当前时间起 15 分钟

javascript - 同时进行ajax调用

javascript - 从 $.(ajax) 获取单选按钮值并将其传递到 SQL 查询中

php - 根据提交的位置在城市、州、国家/地区输入之间插入逗号

php - PHP中如何获取后台进程的PID?

php - 使一段代码在函数和类中工作

javascript - Fullpage.js 中的 Parallax.js 在 Mac Safari 上产生意外结果

javascript - 如何使用 jasmine Ajax stub AJAX 函数