mercredi 30 mars 2016

Laravel - Pass AJAX data to function

I am working on a very simple Laravel application that basically presents a graph based on an id. I believe I have set up the correct route using:

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

From there I went on to set up the correct function in the controller:

app\Http\Controllers\HonoursController.php

...
    public function getProcessorData()
    {
      $id = Input::get('host_id');
      return Response::json(HostProcessor::get_processor_formatted($id));
    }
...

Since this called a function from the HostProcessor model, I created this function below:

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

Finally I set up my AJAX function such as below:

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

Now the issue I am having here is when I try and pass a value to the HostProcessor function it fails. I have tried passing an id value using the data attribute of AJAX. By doing this I had to update my route to Route::get('getProcessorData?{id}', array('uses' => 'HonoursController@getProcessorData')); but this still failed with a 404 error. I also tried just getting the value of host_id value using $id = Input::get('host_id'); and passing those to the HostProcessor function but that still failed with a 404 error. Any ideas why?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/25xwnYw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire