doutang7661 2017-07-06 10:41
浏览 43

AJAX调用无法从PHP获取数据,显示未定义的数据

I am new to web development and this is my first question here so please don't mind my vagueness, if any.

Now, I am facing facing problem with my javascript file which is supposed to draw a line chart. Through an ajax call I am unable to fetch data from a php file which gets some variable from a html form. The problem is that I am not getting anything when I load my test.html (which includes the necessary file) not even in console.

Relevant part of my js file is here (graph.js):

$(document).ready(function(){
$.ajax({
    url : "http://localhost/series/data.php",
    type : "GET",
    dataType: "json",
    success : function(data){
        console.log(data);

        var time = [];
        var sensor1 = [];
        var sensor2 = [];
        var sensor3 = [];

        for(var i in data) {
            time.push("time " + data[i].time);
            sensor1.push(data[i].sensor1);
            sensor2.push(data[i].sensor2);
            sensor3.push(data[i].sensor3);
        }...

And this is my PHP file(data.php):

<?php
 header('Content-Type: application/json');
//connects to database
include '2connect.php';
//gets a date of which sensor data is to be displayed
if(isset($_GET['dateselector'])&&!empty($_GET['dateselector'])){
        $date=$_GET['dateselector'];
        //echo "$date";
    $query1= "SELECT `time`,`sensor1`,`sensor2`,`sensor3` FROM `$date`";

if($result = mysqli_query($con,$query1)){

        $data=array();
        while ($row = mysqli_fetch_assoc($result)) {
            $data[]=$row;
        }

        $result->close();
        $con->close();

        print json_encode($data);
    }else {
        echo"<br> failed <br>";
    }
}else {
    echo "You didn't select a date 
";
}

?>

Whenever I remove the isset check from my php code and give $date a value the code runs perfectly. Is it so that ajax doesn't fetches data from a php file which gets data (form data) from another file?

EDIT: The process is: User chooses a date from the calender (index.html) that date is sent to a php file (data.php) which displays the result in json format. Then in another browser I open a html file(test.html) which includes graph.js and graph.js should fetch data from data.php.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP