local-host 2017-01-16 16:08 采纳率: 100%
浏览 21

获取JSON文件并推送PHP

In my data storage folder is a JSON file. I save the device id to the local storage of the broswer:

//Store deviceID in local storage on button click
function store(){
    var inputDeviceID= document.getElementById("deviceID");
    localStorage.setItem("deviceID", inputDeviceID.value);
}

I get the device id with a javascript function and push it with ajax into my php script to load the corresponsing data:

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
               });
            }
        });
    }
}

PHP code for searching the json file and decode it:

$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'};

But this is not working, how can I get data from the ajax request in the PHP script?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测