I have a very strange problem getting the result of a POST global variable in Symfony 4.
I tried this way :
$date = $request->request->get('date');
This is how I actually send the AJAX request when the Calendar input's date changed:
onSelect: function(date, instance) {
$.ajax({
url : 'home',
type : 'POST',
data : {'date':date},
dataType : 'html',
success : function(code_html, statut){
console.log(statut);
},
error : function(resultat, statut, erreur){
},
complete : function(resultat, statut){
}
});
The onSelect callback successfully receive the date value I want.
And this result shows the 200 success code with right values for the date variable :
But $date is null
.
There is no problem with headers or anything like that in your code, and the jQuery request is correct as it is (I tested your code).
The problem might be with your PHP code, which is not present in your question.
If you are injecting the Request
object correctly, you can easily use get()
to retrieve the post variables.
For example:
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class VeryDefaultController extends AbstractController
{
/**
* @Route("/very/default", name="very_default")
* @param $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(Request $request)
{
$date = $request->get('date');
}
}
In your example you are doing $request->request->get('date')
which doesn't look right. But since you do not show where are you creating $request
, we can't be sure.
The above code is tested with your jQuery AJAX POST request, and it works. There is nothing inherently wrong in that part. And it is perfectly possible to access POST variables using get()
.