title: Yii2: Trying to generate a csv file with ajax
body: <p>Im trying to generate a basic csv file with some data. When I use an alert inside the ajax call it will show me the data(so there is data passing), but when I click the button it will not generate an CSV file. Im new to yii2 so im still learning.</p>
  
  <p><strong>UPDATED</strong>
  I have changed the files</p>
  
  <p>//view export/index.php</p>
  
  <pre><code>Pjax::begin();
  
  $form = ActiveForm::begin([
      'action'  =&gt; yii\helpers\Url::to(['cms-export/index']),
      'options' =&gt; ['data' =&gt; ['pjax' =&gt; true]],
      'layout'  =&gt; 'horizontal',
      'fieldConfig' =&gt; [
          'horizontalCssClasses' =&gt; [
              'label'   =&gt; 'col-sm-2',
              'offset'  =&gt; 'col-sm-offset-2',
              'wrapper' =&gt; 'col-sm-5',
              'hint'    =&gt; 'col-sm-5',
          ],
      ],
  ]);
  
      echo $form-&gt;field($model, 'language')-&gt;dropDownList([//some list]);
  
      echo $form-&gt;field($model, 'filename')-&gt;textInput()
  
      echo Html::submitButton('Submit', ['class' =&gt; 'btn btn-primary'])';
  
  
  
  ActiveForm::end();
  
  Pjax::end();
  </code></pre>
  
  <p>//model</p>
  
  <pre><code>public function generateCsv(){
  
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="sample.csv"');
  
      $data = [datacomeshere];
  
      $fp = fopen('php://output', 'w');
      foreach ( $data as $line ) {
          fputcsv($fp, $line, ';');
      }
      fclose($fp);
  
  }
  </code></pre>
  
  <p>//controller</p>
  
  <pre><code>public function actionIndex()
  {
  
      $model = new Export();
  
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;validate()) {
  
          // validation works, but method does not work
          \common\models\Export::generateCsv();
  
      }
  
      return $this-&gt;render('index' , ['model' =&gt; $model]);
  
  }
  </code></pre>
  
  <p>When I click the button it will show me an 500 error in the jquery file</p>
  
  <pre><code>xhr.send( options.hasContent &amp;&amp; options.data || null );
  </code></pre>