title: Yii framework 2.0 AJAX request with POST method
body: <p>On a domain I have my Yii 2.0 web application with a method in a controller that handles the POST request. Below is de code inside my controller class.</p>
  
  <pre><code>public function behaviors()
  {
      return [
          'access' =&gt; [
              'class' =&gt; AccessControl::className(),
              'rules' =&gt; [
                  [
                      'actions' =&gt; [
                          'my-method',
                      ],
                      'allow' =&gt; true,
                  ],
              ],
          ],
           'verbs' =&gt; [
               'class' =&gt; VerbFilter::className(),
               'actions' =&gt; [
                   'my-method' =&gt; ['post'],
               ],
           ],
      ];
  }
  
  public function actionMyMethod()
  {
      Yii::$app-&gt;controller-&gt;enableCsrfValidation = false;
  
      $response = Yii::$app-&gt;response;
      $response-&gt;format = \yii\web\Response::FORMAT_XML;
  
      return ['msg' =&gt; 'OK!'];
  }
  </code></pre>
  
  <p>On another domain I have a simple page (Not Yii 2.0 application) that sends an AJAX request with POST method using pure JavaScript. Below is the code.</p>
  
  <pre><code>var xmlhttp;
  if (window.XMLHttpRequest) {
       xmlhttp = new XMLHttpRequest();
  } else {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("POST", "http://my-domain.com/controller-name/my-method", true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp.send("id=Henry");
  xmlhttp.onreadystatechange = function() {
       if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) {
            var responseXml = xmlhttp.responseXML;
            txt=" ";
            x=responseXml.getElementsByTagName("msg");
            for (i=0;i&lt;x.length;i++) {
                 txt = txt + x[i].childNodes[0].nodeValue;
            }
            alert(txt);
       }
  }
  </code></pre>
  
  <p>Everytime I tried to run this above JavaScript code, it said POST 400 (Bad Request) in the Console. If I change from POST to GET, it works fine. Can someone tell me what I missed? What is wrong with the code above?</p>