Javascript 用于fullcalendar的Yii2上的Ajax
我正在为Yii2()使用Fullcalendar,我希望在单击日期时使用Ajax保存事件。数据从dropdownlist进行通信 似乎我的代码在控制器中找不到函数,可能是Url 我的JS视图和控制器上我的funstion Ajax链接:Javascript 用于fullcalendar的Yii2上的Ajax,javascript,ajax,yii2,Javascript,Ajax,Yii2,我正在为Yii2()使用Fullcalendar,我希望在单击日期时使用Ajax保存事件。数据从dropdownlist进行通信 似乎我的代码在控制器中找不到函数,可能是Url 我的JS视图和控制器上我的funstion Ajax链接: <?php $form = ActiveForm::begin(); ?> <div class="row"> <div class="col-md-4">
<?php
$form = ActiveForm::begin();
?>
<div class="row">
<div class="col-md-4">
<?= $form->field($feuille_de_jour_responsable, 'ID_Categorie')->dropDownList(CategorieFdj::find()->select(['Nom', 'ID_Categorie'])->indexBy('ID_Categorie')->column(), ['id'=>'catId']); ?>
</div>
<div class="col-md-4">
<?= $form->field($feuille_de_jour_responsable, 'ID_Poste_FDJ')->dropDownList(PosteFdj::find()->select(['Nom_Poste_FDJ', 'ID_Poste_FDJ'])->indexBy('ID_Poste_FDJ')->column(), ['id'=>'postId']); ?>
</div>
<div class="col-md-4">
<?= $form->field($feuille_de_jour_responsable, 'Code_Personnel')->dropDownList(Personnel::find()->select(['Nom_Personnel', 'Code_Personnel'])->indexBy('Code_Personnel')->column(), ['id'=>'codePers']); ?>
</div>
</div>
<?php ActiveForm::end();?>
<?php
$JSCode = <<<EOF
function(start,end) {
//alert ($("select[id=catid] option:selected").text());
var title = $("select[id=codePers] option:selected");
var codePersonnel = $("select[id=codePers] option:selected").val();
var posteId = $("select[id=postId] option:selected").val();
var categorieID = $("select[id=catId] option:selected").val();
//alert($('#catid').val());
var eventData;
var obj = {
Date_Calendaire : start.format(),
ID_Poste_FDJ : posteId,
ID_Categorie : categorieId,
Code_Personnel : codePersonnel
};
$.ajax({
url : 'index.php?r=feuille-de-jour-responsable/create',
dataType: 'json',
data: obj,
success: function (data, response, event, date) {
alert("success here");
/*$('#calendar').fullCalendar('renderEvent',
{
title: title,
start: start.format()
//end: thedate1
}, true);
eventData = {
title: title,
start: start.format(),
end: start.format(),
};
$('#calendar').fullCalendar('renderEvent', eventData, true);*/
},
error: function () {
alert("Oops! Something didn't work");
}
});
}
EOF;
$JSEventClick = <<<EOF
function(calEvent, jsEvent, view) {
alert('Event: ' + calEvent.title);
alert('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
alert('View: ' + view.name);
// change the border color just for fun
//$(this).css('border-color', 'red');
}
EOF;
?>
<?= yii2fullcalendar\yii2fullcalendar::widget([
'id' => 'calendar',
'clientOptions' => [
'height' => 650,
// 'language' => 'fa',
//'eventLimit' => TRUE,
'selectable' => true,
'selectHelper' => true,
'droppable' => true,
'editable' => true,
// 'theme'=>true,
'fixedWeekCount' => false,
'defaultDate' => date('Y-m-d'),
'eventClick' => new JsExpression($JSEventClick),
'select'=>new JsExpression($JSCode)
],
]);
?>
<?= Html::encode($JSCode); ?>
<?= Html::encode($JSEventClick); ?>
我开始使用firebug,当我单击时,它不会被保存。。。没有错误。
我考虑的是带有“POST”的链接(这与表单中的“普通”链接相同),我的函数“create”将保存数据,但什么也没有发生。“如果”不起作用,我不知道为什么
我发现异常:“SyntaxError:unexpected token<在JSON中的位置0”
谢谢你的帮助()
Sarah对于您的情况,失败的
保存
操作的返回值不太有效
if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ['success' => $feuille_de_jour_responsable->save()];
} else {
return $this->render('create', [
'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
]);
}
您需要以JSON格式返回保存结果,但不返回页面的HTML代码。由于您试图显示整个HTML内容,因此在JSON中的位置0处得到了SyntaxError:unexpected token>
要成功运作:
return ['success' => true];
return ['success' => false];
顺便说一下,您不能在返回响应中再次使用$feuille\u de\u jour\u responsible->save()
,因为它将尝试保存两次
对于失败的操作:
return ['success' => true];
return ['success' => false];
您不能使用render
,因为这是一个完整的页面,您正在使用Ajax请求。失败的save
操作的返回值在您的情况下不太有效
if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ['success' => $feuille_de_jour_responsable->save()];
} else {
return $this->render('create', [
'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
]);
}
您需要以JSON格式返回保存结果,但不返回页面的HTML代码。由于您试图显示整个HTML内容,因此在JSON中的位置0处得到了SyntaxError:unexpected token>
要成功运作:
return ['success' => true];
return ['success' => false];
顺便说一下,您不能在返回响应中再次使用$feuille\u de\u jour\u responsible->save()
,因为它将尝试保存两次
对于失败的操作:
return ['success' => true];
return ['success' => false];
您不能使用render
,因为这是一个完整的页面,您使用的是Ajax请求。如果您可以显示屏幕截图或至少显示错误消息,以便知道在哪里可以查看,那就更好了。:)没有错误,只是截图上什么都没发生你只会看到日历…我用firebug图片更新我的帖子。如果没有错误,那么也许你得到的不是你期望的?我什么都没有得到。。。但我不明白为什么:(如果你能显示屏幕截图或至少你的错误信息,以便知道在哪里可以查看,那就更好了。)没有错误,只是屏幕截图上什么都没有发生,你只会看到日历…我用firebug图片更新我的帖子。如果没有错误,那也许你得到的不是你所期望的?我什么也得不到。。。但我不明白为什么:(谢谢你的回答!我明白。我尝试过这个,但现在我有了“回复内容不能是数组。”我不知道为什么,但我一直在搜索。现在这取决于你想看到什么。你只需在返回中写入true
或false
,你就不会有数组。我不明白:/n这是我的错误。我只想将数据保存在数据库中,然后停留在这一页;)所以<代码>返回true
成功,返回false代码>失败?当我这样做时,我有一个空白页:/谢谢你的回答!我理解。我尝试了这个,但现在我有了“响应内容不能是数组”。我不知道为什么,但我一直在搜索。现在这取决于你想看到什么。您只需在返回中写入true
或false
,就不会有数组。我不理解:/n这是我的错误。我只想将数据保存到我的数据库中,然后停留在此页面;)所以<代码>返回true
成功,返回false代码>失败?当我这样做时,我有一个空白页:/