【发布时间】:2016-07-19 06:38:15
【问题描述】:
我正在开发一个非常简单的 Laravel 应用程序,它基本上呈现基于 id 的图形。我相信我已经使用以下方法设置了正确的路线:
app\Http\routes.php
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('getProcessorData', array('uses' => 'HonoursController@getProcessorData'));
#Route::get('getProcessorData/{id}', 'HonoursController@getProcessorData');
Route::get('getBandwidthData', array('uses' => 'HonoursController@getBandwidthData'));
Route::get('getMemoryData', array('uses' => 'HonoursController@getMemoryData'));
Route::resource('honours','HonoursController');
从那里我继续在控制器中设置正确的功能:
app\Http\Controllers\HonoursController.php
...
public function getProcessorData()
{
$id = Input::get('host_id');
return Response::json(HostProcessor::get_processor_formatted($id));
}
...
由于这从 HostProcessor 模型中调用了一个函数,所以我在下面创建了这个函数:
app\HostProcessor.php
...
public static function get_processor_formatted($id)
{
$processor = self::findorfail($id);
$json = array();
$json['cols'] = array(
array('label' => 'Time', 'type' => 'string'),
array('label' => 'Kernel Space Usage', 'type' => 'number'),
array('label' => 'User Space Usage', 'type' => 'number'),
array('label' => 'IO Space Usage', 'type' => 'number')
);
foreach($processor as $p){
$json['rows'][] = array('c' => array(
array('v' => date("M-j H:i",strtotime($p->system_time))),
array('v' => $p->kernel_space_time),
array('v' => $p->user_space_time),
array('v' => $p->io_space_time)
));
}
return $json;
}
...
最后我设置了我的 AJAX 功能,如下所示:
resources/views/honours/partials/master.blade.php
...
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var processor_usage = $.ajax({
url:'getProcessorData',
dataType:'json',
async: false
}).responseText;
var p_options = {
title: 'Processor Usage',
width: 800,
height: 400,
hAxis: {
title: 'Time',
gridlines: {
count: 5
}
}
};
...
现在我遇到的问题是,当我尝试将值传递给 HostProcessor 函数时它失败了。我尝试使用 AJAX 的 data 属性传递一个 id 值。通过这样做,我不得不将我的路由更新到Route::get('getProcessorData?{id}', array('uses' => 'HonoursController@getProcessorData'));,但这仍然失败并出现 404 错误。我还尝试使用 $id = Input::get('host_id'); 获取 host_id 值并将其传递给 HostProcessor 函数,但仍然失败并出现 404 错误。任何想法为什么?
【问题讨论】:
-
@OmeCoatl #Route::get('getProcessorData/{id}', 'HonoursController@getProcessorData');目前已退出 ahd 对代码没有影响
标签: php jquery ajax laravel laravel-5