In my Yii2 application I am tryng to send bootstrap multiselect dropdown values to my controller and according to the arra sent perform actions in the controller. It always hits the error, I have alerted the thrownError. I get the error as Syntax Error: Unexpected token < in Json at position 4
I cannot understand where am I going wrong.
my dropdown list code
<select id="lstFruit" multiple="multiple" style="width:300px;">
<optgroup label ="General">
<?php for ($i = 0; $i<$genCount; $i++){?>
<option value="<?php echo $genRole[$i] ?>">
<?php echo $genRole[$i] ?>
</option>
<?php } ?>
</optgroup>
<optgroup label ="Chemicals">
<?php for ($i = 0; $i<$chemCount; $i++){?>
<option value="<?php echo $chemRole[$i] ?>">
<?php echo $chemRole[$i] ?>
</option>
<?php } ?>
</optgroup>
<optgroup label ="Risk Assessment">
<?php for ($i = 0; $i<$riskCount; $i++){?>
<option value="<?php echo $riskRole[$i] ?>">
<?php echo $riskRole[$i] ?>
</option>
<?php } ?>
</optgroup>
</select>
My ajax request
jQuery("body").on("click", ".rolesEdit", function() {
var roles = $("#lstFruit").val();
alert(roles.length);
$.ajax({
type: "POST",
cache: false,
data:{roles: roles},
url: "'.$url.'",
dataType: "json",
success: function(data){
if(data.status){
alert(data);
}else{
alert(data);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
if(thrownError == "Forbidden"){window.location = "http://test.qsims.com/index.php/product/check-file"; }
}
});
});
my controller
if(Yii::$app->request->post('roles')){
$p = Yii::$app->request->post();
$roles = $p["roles"];
$count = count($roles);
\app\modules\auth\models\SimAuthAssignment::deleteAll(['in','user_id', $id]);
for($i = 0; $i < $count; $i++){
$auth = Yii::$app->authManager;
$authorRole = $auth->getRole($roles[$i]);
$auth->assign($authorRole, $model->user_id);
}
echo BaseJson::encode([
'status'=>true,
'id'=>$model->user_id,
]);
}
Where am I going wrong? Can anyone help me with this?