为什么我的jQuery Ajax响应首先包含post数组?

时间:2015-04-07 22:56:00

标签: php jquery ajax

我有一个jQuery Ajax POST请求转到PHP站点,并在_POST数组中添加了一些数据。 我无法弄清楚为什么在Ajax响应中我似乎在我的JSON响应之前将_POST数组返回给我。我想要的只是下面回复中的第二行。

$.ajax({
 type: "POST",  
 url: "site.php",  
 data: { requestType : 'someType', table : 'someTable' },
 success: function(data){ 
  alert(data);
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
  alert(errorThrown); //or whatever
 }
});  

回应:

array(2) { ["requestType"]=> string(6) "someType" ["table"]=> string(17)    "someTable" } 
[{"User":1,"User":"xxx","Pd":"xxx","Name":"xxx","Age":xx,"Occupation":"xxx","Description":"xxx"}]

PHP

$result = $stmt->execute(); 

//put the results in to the $result variable
$result = $stmt->get_result(); 

while ($row = $result->fetch_assoc()) {                     
    array_push($result_array, $row);
}

echo json_encode($result_array);

2 个答案:

答案 0 :(得分:0)

使用以下代码使用$item变量中的值填充变量$row

  while ($row = $result->fetch_assoc()) {    
        $item['Pd'] = $row['Pd'];  
        $item['Name'] = $row['Name'];            
        array_push($result_array, $item);
    }

答案 1 :(得分:-1)

尝试更改此代码: success: function(data){ alert(data); }

success: function(response){ alert(response); }

请记住,清除缓存。