1

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?

Mohan Prasad
  • 682
  • 1
  • 9
  • 34

1 Answers1

0

The syntax error is causing because of my url. My url contains a callback ?=

$url=Yii::$app->getUrlManager()->createUrl("user/update?id=".$id);

This is a magic string which activates JSONP functionality.

I created a new action in the controller and changed the url to the newly created action which resolved the issue for me.

  $url=Yii::$app->getUrlManager()->createUrl("user/roles-ajax");

Answer refrenced from enter link description here - @Abdul

Community
  • 1
  • 1
Mohan Prasad
  • 682
  • 1
  • 9
  • 34