i want to persist an entity and catch a message with ajax (without Jquery), but i got troubles with boolean $request->isXmlHttpRequest() .
The script here :
var xhr = getXMLHttpRequest();
xhr.open("POST", "{{ path('deplacement') }}", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("coordonnee="+abs+"-"+ord+"&name="+personnage[0].getAttribute('id'));
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
var classPersonnage = personnage[0].getAttribute('class');
var arrayClassPersonnage = classPersonnage.split(' ');
var id = personnage[0].getAttribute('id');
personnage[0].removeAttribute('class');
var newClass= '';
for (i=0; i < arrayClassPersonnage.length-1; i++){
newClass= newClass+' '+arrayClassPersonnage[i];
}
document.getElementById(id).setAttribute('class', newClass);
var resultat = JSON.parse(xhr.responseText);
alert(resultat['message']);
}
My controller php here
/**
* @Route("/game/combat/test", name="deplacement")
*/
public function deplacementAction(Request $request){
if($request->isXmlHttpRequest())
{
$response = new JsonResponse();
return $response->setData(array('message' => 'Ok.'));
}
else{
$response = new JsonResponse();
return $response->setData(array('message' => 'false'));
}
}
I got always 'false'. I tried to do this :
$response = new JsonResponse();
return $response->setData(array('message' => 'Ok.'));
So without $request->isXmlHttpRequest(), but i catch a internal error server 500. Please help me, i become crazy.