Javascript 获取JSON文件并推入PHP
在我的数据存储文件夹中有一个JSON文件。我将设备id保存到broswer的本地存储器中:Javascript 获取JSON文件并推入PHP,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,在我的数据存储文件夹中有一个JSON文件。我将设备id保存到broswer的本地存储器中: //Store deviceID in local storage on button click function store(){ var inputDeviceID= document.getElementById("deviceID"); localStorage.setItem("deviceID", inputDeviceID.value); } 我使用javascript函
//Store deviceID in local storage on button click
function store(){
var inputDeviceID= document.getElementById("deviceID");
localStorage.setItem("deviceID", inputDeviceID.value);
}
我使用javascript函数获取设备id,并使用ajax将其推送到我的php脚本中,以加载相应的数据:
function localstoragecheck(){
if (localStorage.getItem("deviceID") === null) {
//alert('no');
}
else {
//alert('yes');
var deviceIDcookie = localStorage.getItem("deviceID");
$.ajax({
url: '/',
data: {'deviceID': deviceIDcookie},
dataType: 'jsonp',
complete : function(){
// alert(this.url)
$.getJSON("/sigfoxdata/" + deviceIDcookie + ".json", function(json) {
console.log(json); // this will show the info it in firebug console
});
}
});
}
}
用于搜索json文件并对其进行解码的PHP代码:
$FOLDER = '../sigfoxdata/';
$dir = scandir($FOLDER);
$file = $_GET['deviceID'].'.json' ?: 'DEMO.json';
$fileUrl = $FOLDER.$file;
$file = fopen($fileUrl, "rip");
$lines = fread($file, filesize($fileUrl));
$data = json_decode($lines)->{'data'};
但是这不起作用,如何从PHP脚本中的ajax请求中获取数据?您是否尝试过使用
print\r($\u-get)输出$\u-get
代码>?这将向您显示实际发送到PHP文件的内容。为什么要使用jsonp
?如果文件的内容已经是JSON,就不需要进行JSON解码。只需用$data=file\u get\u contents($fileUrl)替换打开和读取代码>。顺便说一句,您应该事先检查该文件是否存在,并且PHP进程是否可读。@YvesLeBorgfile\u get\u contents
不会自动解码JSON。@Barmar OP不断提到JSON文件
。。。我认为文件的内容已经是json了。fopen()
的'rip'
参数是什么r
用于读取模式,但i
和p
是什么?