尝试通过ajax向我的symfony conbtroller发送数据时我遇到了麻烦,但是似乎我没有在控制器上收到它,甚至无法正确到达它也无法对其进行测试。
我正在使用symfony 3.3,我已经在这里尝试了关于stackoverflow的文档和其他答案,但是我无法解决它,所以有人可以让我知道我在做错什么吗?>
在树枝视图中显示我的JS脚本:
$.blockUI({ message: '<h4><img src="{{ asset('assets/global/plugins/cubeportfolio/cubeportfolio/img/cbp-loading.gif') }}" /> Guardando datos, por favor espere...</h4>' });
var form = document.getElementById("idUsuario").value;
$.ajax({
url: '{{ path('followingAction') }}',
type: 'POST',
data: {form: form},
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
Notificacion("success", "Survey Project", "Empezaste a seguir a este usuario");
$.unblockUI();
$('#sample_2').load(window.location + ' #sample_2 > *');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
Notificacion("error", "Survey Project", "Ha existido un problema y no se ha podido seguir a este usuario.");
$.unblockUI();
}
});
这是我控制器中的代码,我无法执行var_dump或看到任何错误或任何内容:
* @Route("/following/", name="followingAction")
* Method({"GET", "POST"})
* @param Request $request
* @return Response
*/
public function followingAction(Request $request)
{
$request->isXmlHttpRequest();
$id = $request->query->get('form');
var_dump("doesn't work");die;
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('ModelBundle:User')->findOneBy(array('id' => $id ));
$seguidor = $em->getRepository('ModelBundle:Followers')->findOneBy(array('id' => $id ));
$seguido = $em->getRepository('ModelBundle:Followers')->findOneBy(array('id' => $id ));
$follower = new Followers();
//$follower->setUsername($seguidor->getUsername());
$follower->setUserId($seguidor->getId());
$follower->setSeguidoId($user->getId());
$em->persist($follower);
$em->flush();
$response = array("code" => 100, "success" => true);
$response = new Response(json_encode($response));
$response->headers->set('Content-Type', 'application/json');
return $response;
这是我在控制器中的导入:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use ModelBundle\Entity\User;
use ModelBundle\Entity\Categoria;
use ModelBundle\Entity\Followers;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;