title: How to return ajax with separate API URL response to controller in YII2?
body: <p>I am trying to call a API URL using AJAX. I need to validate the response and update the DB, SO I need to return it to the controller.
  Is there any way to do that. Here is my view, JS and controller code.</p>
  
  <p>Here is my View Code where I have a separate URL for validation, which is the API URL 
  View</p>
  
  <pre><code>    &lt;?php $form = ActiveForm::begin([
      'action' =&gt; ['users/renderstep3'],
      'validationUrl' =&gt; 'API URL',
      'options' =&gt; [
          'class' =&gt; 'comment-form'
           ]
        ]); ?&gt;
        &lt;?= $form-&gt;field($paymentmodel, 'customerId')-&gt;hiddenInput(['value'=&gt; $userid])-&gt;label(false) ?&gt;
        &lt;?= $form-&gt;field($paymentmodel, 'owner')-&gt;textInput(['maxlength' =&gt; true]) ?&gt;
       &lt;div class="form-group"&gt;
             &lt;?= Html::submitButton('Submit', ['class' =&gt; 'btn btn-success']) ?&gt;
       &lt;/div&gt;
       &lt;?php ActiveForm::end(); ?&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>    jQuery(document).ready(function($) {
      $('body').on('submit', '.comment-form', function(event) {
          event.preventDefault(); // stopping submitting
          var data = $(this).serializeArray();
          data.splice(0,1);
          var result = {};
  
          for ( i=0 ; i &lt; data.length ; i++)
              {
              key = data[i].name.replace("UserPaymentDetails[", "").slice(0,-1);
              result[key] = data[i].value;
              }
          var url = $(this).attr('validationUrl');
          $.ajax({
             url: url,
              type: 'post',
              dataType: 'json',
              data: JSON.stringify(result)
          })
          .done(function(response) {
                  return response;
          })
          .fail(function() {
              console.log("error");
          });
  
      });
  });
  </code></pre>
  
  <p>Controller Action</p>
  
  <pre><code>       public function actionRenderstep3()
           {
           $model = new Users();
           $detailsmodel = new UserDetails();
           $paymentmodel = new UserPaymentDetails();
  
          if (Yii::$app-&gt;request-&gt;isAjax) {
          Yii::$app-&gt;response-&gt;format = \yii\web\Response::FORMAT_JSON;
          $data = Yii::$app-&gt;request-&gt;post();
          print_r($data) ; exit;
          }
  
          if ($paymentmodel-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $paymentmodel-&gt;validate()) 
          {
          $paymentmodel-&gt;Status = 0;
          $paymentmodel-&gt;save();
          return $this-&gt;redirect(['index']);
          }
          return $this-&gt;render('renderstep3', [
         'model' =&gt; $model,
         'detailsmodel' =&gt; $detailsmodel,
         'paymentmodel' =&gt; $paymentmodel,
          ]);   }
  </code></pre>
  
  <p>Thanks in advance!!</p>