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.
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => [
'my-method',
],
'allow' => true,
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'my-method' => ['post'],
],
],
];
}
public function actionMyMethod()
{
Yii::$app->controller->enableCsrfValidation = false;
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_XML;
return ['msg' => 'OK!'];
}
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.
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 && xmlhttp.status==200) {
var responseXml = xmlhttp.responseXML;
txt=" ";
x=responseXml.getElementsByTagName("msg");
for (i=0;i<x.length;i++) {
txt = txt + x[i].childNodes[0].nodeValue;
}
alert(txt);
}
}
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?