我有一个php脚本,它从服务器获取数据并返回array / json。直接(在浏览器中输入URL)访问此脚本将返回所需的结果(array / json)。
但当我使用ajax返回数组/ json访问相同的脚本时,会进行修剪。例如
这是直接访问脚本
的输出Array
(
[user_id] => Array
(
[0] => 46
[1] => 16
)
[name] => Array
(
[0] => Sewak
[1] => Ram Bachan
)
[date] => Array
(
[0] => 2016-03-04
[1] => 2016-03-04
)
[zone] => Array
(
[0] => North
[1] => North
)
[state] => Array
(
[0] => Punjab
[1] => Punjab
)
[city] => Array
(
[0] => Patiala
[1] => Amritsar
)
[outlet] => Array
(
[0] => asdfasdf
[1] => Malout Road 1
)
)
来自Ajax的响应
Array
(
[user_id] => Array
(
[0] => 16
)
[name] => Array
(
[0] => Ram Bachan
)
[date] => Array
(
[0] => 2016-03-04
)
[zone] => Array
(
[0] => North
)
[state] => Array
(
[0] => Punjab
)
[city] => Array
(
[0] => Amritsar
)
[outlet] => Array
(
[0] => Malout Road 1
)
)
你可以看到每个数组中有两个集合,ajax只从resuts返回一个集合(最后一个)
这是php脚本
header("Content-Type: application/json; charset=UTF-8");
require_once('../../db/conn.php');
//print_r($_REQUEST);
$zone = $_REQUEST['zones'];
$state = $_REQUEST['state'];
$state = "Punjab";
$city = $_REQUEST['city'];
$vmId = $_REQUEST['vm'];
$approval_status = $_REQUEST['approval_status'];
$dealersName = $_REQUEST['outlet'];
$from_date = str_replace("-", "/", $_REQUEST['from_date']);
$to_date = str_replace("-", "/", $_REQUEST['to_date']);
//Creating SQL query
$sql = "SELECT * FROM uploads WHERE 1=1 ";
if (!empty($zone)) {
$sql .= "AND zone='$zone'";
}
if (!empty($state)) {
$sql .= "AND state='$state'";
}
if (!empty($city)) {
$sql .= "AND city='$city'";
}
if (!empty($vmId)) {
$sql .= "AND user_id='$vmId'";
}
if (!empty($dealersName)) {
$sql .= "AND outlet_name='$dealersName'";
}
if (!empty($approval_status)) {
if ($approval_status != "all") {
$sql .= "AND approved='$approval_status'";
}
}
if (!empty($from_date)) {
$sql .= "AND dateTime >= '$from_date'";
}
if (!empty($to_date)) {
$sql .= "AND dateTime <='$to_date'";
}
//echo $sql;
$searchResults = mysqli_query($conn, $sql) or die("Search Failed = >" . mysqli_error($conn));
$id = array();
$name = array();
$date = array();
$zone = array();
$state = array();
$city = array();
$outlet = array();
while ($data = mysqli_fetch_array($searchResults)) {
array_push($id, $data['user_id']);
array_push($name, $data['name']);
array_push($date, $data['dateTime']);
array_push($zone, $data['zone']);
array_push($state, $data['state']);
array_push($city, $data['city']);
array_push($outlet, $data['outlet_name']);
}
$jsonData = array(
"user_id" => $id,
"name" => $name,
"date" => $date,
"zone" => $zone,
"state" => $state,
"city" => $city,
"outlet" => $outlet
);
print_r($jsonData);
//echo json_encode($jsonData);
exit();
这是Javascript / Ajax
function getResults() {
loader.style.visibility = "visible";
var zoneValue = zone;
var stateValue = state;
var cityValue = city.value;
var vmValue = vm.value;
var outletValue = outlet.value;
for (var i = 0; i < approved.length; i++) {
if (approved[i].checked) {
approvedValue = approved[i].value;
break;
}
}
var fromValue = from.value;
var toValue = to.value;
var params = "query=1&zones=" + zoneValue + "&state=" + stateValue + "&city=" + cityValue + "&vm=" + vmValue + "&approval_status=" + approvedValue + "&outlet=" + outletValue + "&from_date=" + fromValue + "&to_date=" + toValue;
console.log(zoneValue, stateValue, cityValue, vmValue, outletValue, approvedValue, fromValue, toValue);
var ajax = new XMLHttpRequest();
ajax.open("POST", "includes/masterData.php", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.onreadystatechange = function () {
if (ajax.readyState == 4 && ajax.status == 200) {
console.log(ajax.responseText);
loader.style.visibility = "hidden";
}
};
ajax.send(params);
}