>我必须使用单个AJAX POST从多个文本文件中获取多个数据,以便可以最小化HTTP请求/帖子,并在低带宽上顺利处理网页。
这是我的代码:
HTML/jQuery:
var time = 0;
var refreshTime = ''; // refreshTime = setInterval -> refresh()
var arr = ['one@one.com','two@two.com','three@three.com'] //for example, I use only 3 emails
function refresh(){
$.ajax({
url: 'fetch.php?action=detail",
type: 'POST',
data: 'time='+time+'&to='+arr+'&from=forme@demo.com', //sending emails as `to`, emails separated with `,`. `to` and `from` is the name of `dir` and `txt` file.
dataType: 'json',
cache: false,
success: function(response){
if(response.length) {
for(var i in response) {
alert(response[i]);
}
}
},
error: function(jqXHR,textStatus,errorThrown){
alert(textStatus+"-"+errorThrown);
}
});
}
.PHP:
define('INCLUDE_CHECK',true);
include('functions.php');
header('Content-Type: application/json');
$data = array();
switch($_GET['action']) {
case 'detail':
$from = $_POST['from'];
$to = explode(',', $_POST['to']); //exploding the `,`
foreach($to as $key){
// check `dir` and than check `text file`
if(!is_dir("details/".$key)&&(!file_exists("details/".$key."/".$from.".txt") || 0 == filesize("details/".$key."/".$from.".txt"))){
$file = "details/".$from."/".$key.".txt";
}else{
$file = "details/".$key."/".$from.".txt";
}
// when I used `data[] = $file;` here and remove all below code, than it alerts txt file names.
// I think that the problem started from here
$arr = file($file);
if(!$_POST['time']){$_POST['time'] = 0;}
foreach($arr as $row) {
$aTemp = null;
list($aTemp['time'], $aTemp['date'], $aTemp['fromname'], $aTemp['to'], $aTemp['msg']) = explode('|', $row);
if($aTemp['msg'] AND $aTemp['time'] > $_POST['time']){ // checking the time, if `POST` time is lower than the `$aTemp['time']` then add the contents to `$data`
$data[] = $aTemp;
}
}
}
break;
}
echo json_encode($data);
firebug
显示了[]
反应。
如果我使用 ajax POST 发送一封电子邮件来做同样的事情,这就是工作代码
(这样做的缺点是:如果有 4 封电子邮件,那么我必须初始化它 4 次,它会发送 4 个查询 - 更多电子邮件,更多查询 - 带宽负担)。
var time = 0, refreshTime = '';
function refresh(to){
$.ajax({
url: 'fetch.php?action=detail",
type: 'POST',
data: 'time='+time+'&to='+to+'&from=forme@demo.com',
dataType: 'json',
cache: false,
success: function(response){
if(response.length) {
for(var i in response) {
alert(response[i]);
}
}
},
error: function(jqXHR,textStatus,errorThrown){
alert(textStatus+"-"+errorThrown);
}
});
}
case 'detail':
$from = $_POST['from'];
$to = $_POST['to'];
if(!is_dir("details/".$to)&&(!file_exists("details/".$to."/".$from.".txt") || 0 == filesize("details/".$to."/".$from.".txt"))){
$file = "details/".$from."/".$to.".txt";
}else{
$file = "details/".$to."/".$from.".txt";
}
$arr = file($file);
if(!$_POST['time']){$_POST['time'] = 0;}
foreach($arr as $row) {
$aTemp = null;
list($aTemp['time'], $aTemp['date'], $aTemp['fromname'], $aTemp['to'], $aTemp['msg']) = explode('|', $row);
if($aTemp['msg'] AND $aTemp['time'] > $_POST['time']){
$data[] = $aTemp;
}
}
}
break;
对此有什么建议或解决方案吗?
更新:最接近答案,但不完美:
.PHP:
case 'view':
$from = $_POST['from'];
$to = explode(',', $_POST['to']);
foreach($to as $key){
if(!is_dir("details/".$to)&&(!file_exists("details/".$to."/".$from.".txt") || 0 == filesize("details/".$to."/".$from.".txt"))){
$file = "details/".$from."/".$to.".txt";
}else{
$file = "details/".$to."/".$from.".txt";
}
$arr = file($file);
if(!$_POST['time']){$_POST['time'] = 0;}
foreach($arr as $row) {
$aTemp = null;
list($aTemp['time'], $aTemp['date'], $aTemp['fromname'], $aTemp['to'], $aTemp['msg']) = explode('|', $row);
if($aTemp['msg'] AND $aTemp['time'] > $_POST['time']){
$n[] = $aTemp;
}
}
$data[$key] = $n;
}
break;
此代码返回 ( firebug console result
):
{
"one@one.com":[
{"msg":"hi","to":"one@one.com","fromname":"one","date":"April 1, 2014","time":"1396376996"},
{"msg":"...","to":"...","fromname":"...","date":"...","time":"..."},
...
],
"two@two.com":[
{"msg":"...","to":"...","fromname":"...","date":"...","time":"..."},
....
]
}
现在我无法猜测如何在jQuery中操作/使用它。像这样:
var arr = ['one@one.com','two@two.com',...];
success: function(response){
if(response.length) {
for(var i in arr){
var a = arr[i];
alert(response.a);
}
}
},
以便它根据 arr = [...]
提醒数组值。
更新: 解决方案 ( jQ part
):
for(var i in response){
console.log(i);
for(var j in response[i]){
console.log(response[i][j]);
}
}
我怎样才能实现它?
目前还不清楚你的问题是什么。如果是 Javascript 部分,并且您想遍历所有消息,您可以这样做:
success: function(response){
for (var from in response){
for (var i = 0, l = response[from].length; i < l; i++) {
var msg = response[from][i];
console.log(msg.fromname + ": " + msg.msg);
}
}
}
还是 PHP 输出不是您想要的?