我一直在试图解决这个问题很长一段时间。我知道JavaScript是客户端脚本,PHP是服务器端脚本,但是可以将浏览器时代传递给PHP脚本来运行一些计算,然后根据结果返回geoJSON数据;全部是异步的?异步传递客户端到服务器的值并返回到客户端
我想传递给PHP脚本的功能是一样的东西:
function retreiveUTC() {
var now = new Date().getTime();
var browserUTC = Math.floor(now)/1000.0; // this returns epoch time of the browser
}
然后我想采取从功能产生了划时代的时间,并把它传递给PHP脚本,以确定哪些JSON文件返回。
比如我会驻留在服务器上的以下文件:
1352188536.JSON 1352187536.JSON 1352177536.JSON
我要检索的一个最接近的浏览器的划时代成果。先谢谢你。
更新下面的代码:
不知道我做错了,但我从Ajax调用成功获得,但返回的值是不确定的。
AJAX的东西:
$.post ('timezone.php',
{
epochTime: browserUTC
},
function (result) {
//do someting to result
}, "json");
var browserUTC = new Date().getTime();
$.post ('timezone.php',
{
epochTime: browserUTC
},
function (result,status) {
//display file name returned by PHP
alert(result.file + "\n" + status);
//console.log('test');
});
PHP:
<?php
$epochTime = round($_POST['epochTime']/1000);
$res= array();
$res['file'] = $epochTime . '.JSON';
return json_encode($res);
?>
我能时代价值传递给PHP脚本并进行处理但'console.log(result.file);'显示未定义。我错过了吗?看起来PHP脚本没有返回值。谢谢。 –
这是因为需要将“json”参数添加到$ .post函数以进行正确的JSON处理。查看编辑后的代码。 – akhilless
这是假设将文件作为JSON对象返回还是仅返回文件名? –