獲取JSON文件并推入PHP
獲取JSON文件并推入PHP,,php,,json,ajax,,Php,,Json,Ajax,在我的數(shù)據(jù)存儲(chǔ)文件夾中有一個(gè)JSON文件。我將設(shè)備id保存到的本地存儲(chǔ)器中://Store in local on store(){var = .("");.("", .value);}我使用函
在我的數(shù)據(jù)存儲(chǔ)文件夾中有一個(gè)JSON文件。我將設(shè)備id保存到的本地存儲(chǔ)器中:
//Store deviceID in local storage on button click
function store(){
var inputDeviceID= document.getElementById("deviceID");
localStorage.setItem("deviceID", inputDeviceID.value);
}
我使用函數(shù)獲取設(shè)備id,并使用ajax將其推送到我的php腳本中php取數(shù)據(jù)后序號(hào)從1開(kāi)始,以加載相應(yīng)的數(shù)據(jù):
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文件并對(duì)其進(jìn)行解碼的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請(qǐng)求中獲取數(shù)據(jù)?
您是否嘗試過(guò)使用
print\r($\u-get)輸出$\u-get
?這將向您顯示實(shí)際發(fā)送到PHP文件的內(nèi)容。為什么要使用
jsonp
?如果文件的內(nèi)容已經(jīng)是JSON,就不需要進(jìn)行JSON解碼。只需用
$data=file\u get\u contents($fileUrl)替換打開(kāi)和讀取代碼>。順便說(shuō)一句,您應(yīng)該事先檢查該文件是否存在,并且PHP進(jìn)程是否可讀。@YvesLeBorgfile\u get\u contents
不會(huì)自動(dòng)解碼JSON。@ OP不斷提到

JSON文件
。。。我認(rèn)為文件的內(nèi)容已經(jīng)是json了。
fopen()
的
'rip'
參數(shù)是什么
r
用于讀取模式php取數(shù)據(jù)后序號(hào)從1開(kāi)始,但
i
和
p
是什么?