jquery 使用spatie/laravel-google-calendar创建Google日历事件

xytpbqjk  于 11个月前  发布在  jQuery
关注(0)|答案(1)|浏览(119)

问题:

我试图使用Laravel和Ajax创建一个事件,但我遇到了一个问题,当我更改开始和结束日期时,什么也没有发生。我在下面提供了我的代码以供参考。

说明:

我有一个Laravel控制器,它有一个**store**方法来处理事件的创建。当我在表单中更改开始和结束日期时,我想使用Ajax将数据发送到服务器并创建事件。但是,它没有按预期工作,我需要帮助确定出了什么问题。

web.php(Route)

Route::resource('event',EventController::class);

字符串

EventController

public function store(Request $request)
    {
     
        if ($request->isMethod('post')) {
            $event = $this->createEventInstance($request);
            
            // Perform other actions if needed
            
            return;
        }

    }
      
    private function createEventInstance(Request $request)
{
    $startTime = Carbon::parse($request->input('effective_period_start_date'));
    $endTime = Carbon::parse($request->input('effective_period_end_date'));

    return new Event([
        'startDateTime' => $startTime,
        'endDateTime' => $endTime
    ]);
}

刀片编码

<div class="col-sm-4">
    <div class="mb-3">
      <label class="form-label">Effective Period Start date</label>
      <input type="date" class="form-control effective_period_start_date" name="effective_period_start_date" value="{{@$obj->effective_period_start_date}}" {{ $disabledfield }}>
    </div>
  </div>
  <div class="col-sm-4">
    <div class="mb-3">
      <label class="form-label">Effective Period End date</label>
      <input type="date" class="form-control effective_period_end_date" name="effective_period_end_date" value="{{@$obj->effective_period_end_date }}" {{ $disabledfield }}>
    </div>
  </div>

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    var url = '/event';
    $(document).ready(function() {
    $('.effective_period_start_date, .effective_period_end_date').on('change', function() {
        var startDate = $('.effective_period_start_date').val();
        var endDate = $('.effective_period_end_date').val();

        if (startDate && endDate) {
            // Prepare the data to send to the server
            // var data = {// Include CSRF token
                
            //     "effective_period_start_date": startDate,
            //     "effective_period_end_date": endDate
            // };
            $.ajax({
                type: 'POST',
                url: url,
                data: {
                    effective_period_start_date: startDate,
                    effective_period_end_date: endDate
                },
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }, // Send data in the request body
                success: function(response) {
                    console.log('Event created:', response);
                    // You can handle the success response here
                },
                error: function(xhr, status, error) {
                    console.error('Error:', error);
                    // You can handle errors here
                }
            });
        }
    });
});

发行详情:

1.我正在使用控制器中的**store方法来创建事件。
1.我有一个私有方法
createEventInstance**来准备事件数据。
1.我使用Js和Ajax在开始和结束日期更改时将数据发送到服务器。

预期行为:

我希望创建事件并处理成功响应。

实际行为:

事件未被创建,我没有收到任何成功响应。

问题:

  • 你能帮我找出导致这个问题的原因吗?
  • 我的Ajax请求或控制器逻辑有什么问题吗?
  • 是否有任何特定的Laravel路由或配置需要检查?
fcipmucu

fcipmucu1#

下面是一个将谷歌日历与laravel集成的例子使用的包“laravel/socialite”

use Google\Client as GoogleClient;
use Google_Service_Calendar;

private function createEventInstance(Request $request)
{
  $client = new GoogleClient();

  //download credentials google cloud, enable google calendar API then create credentials and download
  $client->setAuthConfig(storage_path('app/google/credentials.json'));
  $client->addScope(Google_Service_Calendar::CALENDAR);

  $guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false)));
  $client->setHttpClient($guzzleClient);

  //google oauth will return this "$token" you can save it to database for future use 
  $client->setAccessToken($token);
  $client->refreshToken($tokenRefresh);

  $service = new Google_Service_Calendar($client);

  $obj = array(
        'summary' => 'title',
        'description' => 'notes',
        'start' => [
            'dateTime' => Carbon::parse('start_date_time')->format('Y-m-d\TH:i:s'),
            'timeZone' => 'Australia/Sydney'
        ],
        'end' => [
            'dateTime' => Carbon::parse('end_date_time')->format('Y-m-d\TH:i:s'),
            'timeZone' => 'Australia/Sydney'
        ],
      );

      $service->events->insert('email', $this->makeCalendarEvent($obj));
}

private function makeCalendarEvent($arr)
{
  return new Google_Service_Calendar_Event($arr);
}

字符串
如果我能帮上忙的话请告诉我

相关问题