douqin6785 2017-01-05 14:50
浏览 70

从PHP转换为JSON文件以使用JS绘制图表

i want to extract data from the database and send as JSON file to the front-end and then try to draw graph. i have three tables notification where the website url is saved, status that has status and speed( google page speed) and a pivot table where all data related to a website is stored as values. i have model, and inside the model, i have a function that makes many to many relations between notifications and status table. i have a controller called chart controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ChartController extends Controller
{

     public static function speedHistory(){

        $o_status = Status::where('name','speed')->first();
        $o_response = $this->statuses()->where('status_id', $o_status->id)
        ->select('values AS value', 'created_at AS timestamp')->orderBy('created_at','DESC')->get();


        if($o_response){
            return response()->json($o_response);
        }
            // return an empty json array instead of false
            //return false;
        return response()->json(array());
    }
}

and a front end page called app.js

document.addEventListener('DOMContentLoaded', function(){

    $.getJSON("/json", function (result) {

        alert(result)
    }

and a route

Route::get('json','ChartController@speedHistory');

i use alert to see,if the data is going to app.js but it doesnt work. i would apperciate nay kind of help and suggestions?

this is the view file

extends('layout')

@section('header')
    <a class="logout" href="<?php echo 'login'?>">Logout</a>
    <a href="{{ URL::to('/') }}">               
       <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Crystal_button_cancel.svg/32px-Crystal_button_cancel.svg.png" alt="Submit" width="48" height="48">
    </a>
@stop

@section('content')
<script src="app.js"></script>
    <table class="table">
            <thead> 
            <h3>{{ $notification->website_url }}</h3>
                <tr> 
                    <th>date</th> 
                    <th>status</th>
                </tr> 
            </thead> 
            <tbody>
                <?php $notificationHealth = $notification->history('health'); ?>


                @foreach($notificationHealth as $status)

                    <tr>   
                        <td>{{ $status['timestamp'] }}</td>                         
                        <td> {{ $status['value'] }}</td>.
                    </tr>
                @endforeach
            </tbody>
        </table>

        <canvas id="pageSpeedHistoryChart" width="200" height="50"></canvas>
@stop

@section('footer')

@stop
  • 写回答

1条回答 默认 最新

  • dtrj74376 2017-01-05 14:58
    关注

    Can you add more details?

    With /json you are calling a page under the same domain. Is this right? You can use developer tools of chrome. Under network tab you can see the response of the call that you have done.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题