I've got a 'create' form in Symfony and by putting in some informations in textarea, the controller should just process the request and response it respectfully.
The AJAX call is made in an twig template:
{% block content_foot_script %}
<script>
function fetchData() {
xhttp = new XMLHttpRequest();
xhttp.open("POST", "{{ path('receiptFetchSystem') }}", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = $("#admin_store_receipt_receiptbundle_receipt_objects").val();
xhttp.send('items=' + data);
xhttp.onreadystatechange = function() {
if (xhttp.success && xhttp.code == 200) {
document.getElementById('demo').innerHTML = xhttp.responseText;
}
}
}
</script>
{% endblock %}
and the ReceiptFetchController handles the requests:
<?php
namespace Admin\Store\Receipt\ReceiptBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class ReceiptFetchController extends Controller
{
/**
* @Route("/call", name="receiptFetchSystem")
* @Template()
*/
public function callAction()
{
$request = $this->container->get('request');
$data = $request->request->get('items');
$response = array("code" => 200, "success" => true);
return new Response($response);
}
}
Well, as expected, I'm getting no response :)
Best regards
usestatement for the Symfony Response class?xhttp.success? Also encode your data properly...xhttp.send('items=' + encodeURIComponent(data));if (xhttp.responseType == 4 && xhttp.status == 200) {your response is not attached to the xhr objectfetchData()is called byonchangeevent.