简体   繁体   English

Symfony2中带有CSRF令牌的JQuery的$ .post

[英]JQuery's $.post with CSRF token in Symfony2

I am writing a Symfony2 website, in some cases, it interest me to post a few data through jquery (without creating an HTML form), like this: 我正在编写一个Symfony2网站,在某些情况下,我很想通过jquery发布一些数据(而不创建HTML表单),如下所示:

function deleteNode(route, nodeIdToDelete){
$.post(route, { nodeId: nodeIdToDelete}, function(data){
    console.log(data);
});

} }

The route and the controller and everything is created but the server is responding me: 路由和控制器以及所有内容均已创建,但服务器正在响应我:

POST [route] 500 (Internal Server Error) POST [路由] 500(内部服务器错误)

I suppose that the problem is the CSRF protection, so my questions are: 我想问题是CSRF保护,所以我的问题是:

  1. Is there anyway to use JQuery functions like $.post with some type of generated CSFR token from the controller or something like that? 无论如何,是否可以将JQuery函数(例如$ .post)与从控制器生成的某种类型的CSFR令牌一起使用?

  2. I can disable CSRF protection only for THIS route? 我只能为此路由禁用CSRF保护吗?

Thanks. 谢谢。

For addressing Symfony routes from JavaScript please use FOSJsRoutingBundle. 要使用JavaScript解决Symfony路由,请使用FOSJsRoutingBundle。

https://github.com/FriendsOfSymfony/FOSJsRoutingBundle https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM