简体   繁体   English

从PHP转换为JSON文件以绘制具有JS图表的图表

[英]convert from PHP to JSON file to to draw chart wirth JS

i want to extract data from the database and send as JSON file to the front-end and then try to draw graph. 我想从数据库中提取数据并将其作为JSON文件发送到前端,然后尝试绘制图形。 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 还有一个名为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. 我使用警报来查看,如果数据将发送到app.js,但它不起作用。 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

Can you add more details? 您可以添加更多详细信息吗?

With /json you are calling a page under the same domain. 使用/ json,您将在同一域下调用页面。 Is this right? 这是正确的吗? You can use developer tools of chrome. 您可以使用chrome开发人员工具。 Under network tab you can see the response of the call that you have done. 在“网络”选项卡下,您可以查看已完成的呼叫的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM