繁体   English   中英

在PHP页面中从Ajax JSON ARRAY获取数据

[英]Get back data from ajax JSON ARRAY in php page

我以ajax(json阵列)传送资料

$.ajax({
  type: 'POST',
  url: "http://localhost:8080/rest/test",
  data: JSON.stringify({
    "type":["car"],
    "country": ["Italy", "Canada"],
    "price" : [55 000],
    "colors": ["blue", "black", "red"]
  }),
  success: function(data) {
    console.log("success:", data);  // show an empty array
  },
  failure: function(errMsg) {
     console.error("error:",errMsg);
  }
  dataType: "json",
  contentType: "application/json"
});

当我想在有SQL请求的php页面中获取数据时,我总是得到一个空响应。

function getData() {
    $y = $_POST;
    $array = [];
    $array = array_push($array, $y);
    $j = json_decode($array);
    foreach($j as $k){
        $t = $k->type;
        $c = $k->country;
        $p = $k->price;
        $col = $k->colors;
    }           

    $type = join("','", $t); 
    $country = join("','", $c); 
    $price = join("','", $p); 
    $colors = join("','", $col); 

    global $wpdb;

    $query = "SELECT * FROM vehicles WHERE type IN ('$type') AND (country IN ('$country') OR price IN ('$price') OR colors IN ('$price')) ORDER BY ID DESC";
    $result = $wpdb->get_results($query);
    return $result;
} 

我想要一个像这样的数组:

'[{"type":["car"], "country": ["Italy", "Canada"], "price" : [55 000], "colors": ["blue", "black", "red"]}]'

然后做一个json_decode(this array);

有人可以建议我怎么做吗?

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM