2016-03-31 27 views
0

Temel olarak bir kimliğe dayalı bir grafik sunan çok basit bir Laravel uygulamasında çalışıyorum.Laravel - Çalıştırılacak AJAX verisi

: Ben denetleyicisi doğru fonksiyonunu kurmak için gitti Oradan

uygulaması \ Http \

<?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' => '[email protected]')); 
#Route::get('getProcessorData/{id}', '[email protected]'); 
Route::get('getBandwidthData', array('uses' => '[email protected]')); 
Route::get('getMemoryData', array('uses' => '[email protected]')); 

Route::resource('honours','HonoursController'); 

routes.php: Ben kullanarak doğru rotayı kurdunuz inanıyoruz Bu bir fu denilen bu yana uygulaması \ Http \ Kontrolörler HonoursController.php

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

\ nction HostProcessor modelinden, aşağıda bu işlevi oluşturuldu:

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

Sonunda böyle aşağıdaki gibi benim AJAX işlevini kurmak uygulama \ HostProcessor.php:

kaynaklar/görünümler/onur/partials/Şimdi 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 
       } 
       } 
      }; 
... 

Sorunum Burada sahip olmak, HostProcessor işlevine bir değer vermeyi denediğimde başarısız oluyor. AJAX'ın veri özelliğini kullanarak bir id değeri geçmeyi denedim. Bunu yaparak rotayı Route::get('getProcessorData?{id}', array('uses' => '[email protected]')); olarak güncellemeliydim, ancak bu hala bir 404 hatasıyla başarısız oldu. Ayrıca, $id = Input::get('host_id'); kullanarak host_id değerini almayı ve bunları HostProcessor işlevine aktarmayı denedim, ancak yine de 404 hatasıyla başarısız oldu. Herhangi bir fikir neden?

+0

@OmeCoatl #Route :: olsun ('getProcessorData/{id}', 'HonoursController @ getProcessorData'); şu anda dışarı çıktı ahd kod üzerinde hiçbir etkisi yoktur – NSaid

cevap

1

bu deneyin:

// app\Http\routes.php 
Route::get('getProcessorData/{id}', '[email protected]'); 

// app\Http\Controllers\HonoursController.php 
//... 
public function getProcessorData($id) 
    { 

     try { 

      $processor = HostProcessor::get_processor_formatted($id); 

      return response()->json($processor, 200); 

     } catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e) { 

      return response()->json(['error' => $e->getMessage()], 404); 

     } catch(\Exception $e) { 

      return response()->json(['error' => $e->getMessage()], 500); 
     } 

    } 
//... 
İlgili konular