问题:
我试图使用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路由或配置需要检查?
1条答案
按热度按时间fcipmucu1#
下面是一个将谷歌日历与laravel集成的例子使用的包“laravel/socialite”
字符串
如果我能帮上忙的话请告诉我