问题:
我正在尝试使用 Laravel 和 Ajax 创建一个事件,但遇到一个问题,当我更改开始和结束日期时没有任何反应。我在下面提供了我的代码供参考。
描述:
我有一个 Laravel 控制器,带有
store
方法来处理事件的创建。当我更改表单中的开始日期和结束日期时,我想使用 Ajax 将数据发送到服务器并创建事件。但是,它没有按预期工作,我需要帮助确定出了什么问题。
web.php(路线)
Route::resource('event',EventController::class);
事件控制器
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
}
});
}
});
});
问题详情:
我正在控制器中使用
store
方法来创建事件。
我有一个私有方法
createEventInstance
来准备事件数据。
我使用Js和Ajax在开始和结束日期发生变化时将数据发送到服务器。
预期行为:
我希望创建事件并处理成功响应。
实际行为:
活动尚未创建,我没有收到任何成功响应。
问题:
您能帮我确定可能导致此问题的原因吗?
我的 Ajax 请求或控制器逻辑有什么问题吗?
我需要检查任何特定的 Laravel 路由或配置吗?
这里是一个将 google 日历与 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);
}
请告诉我是否可以帮助您