【问题标题】:Laravel - Pass AJAX data to functionLaravel - 将 AJAX 数据传递给函数
【发布时间】: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' =&gt; '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


【解决方案1】:

试试这个:

// app\Http\routes.php
Route::get('getProcessorData/{id}', 'HonoursController@getProcessorData');

// 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);
      }

    }
//...

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2022-01-01
    • 2014-12-08
    • 2019-07-16
    相关资源
    最近更新 更多