如何使用ajax发布到Symfony2中的安全控制器


how to post with ajax to secured controller in Symfony2

我正试图将Json数据类型发送到安全控制器(is_granted(ROLE_ADMIN)),但是,正如您所猜测的,我无法使其工作,我得到了405 method not allowed响应。

当我把它发送到一个不安全的控制器时,它是有效的,所以我必须让控制器知道帖子是用管理员角色发送的,但如何发送?

这是我的控制器:

/**
* @Route("/admin")
* @Security("is_granted('ROLE_ADMIN')")
*/
class AdminController extends Controller {
    /**
    * Lists all Project entities.
    *
    * @Route("/savesortorder", name="save_sort_order")
    * @Security()
    */
    public function orderAction(Request $request) {
      $response = json_encode(array('message' => 'Saved succesfully!'));
      return new Response($response, 200);
    }

这是我的ajax帖子:

$.ajax({
    url: "/TMC/web/app_dev.php/admin/project/savesortorder",
    type: 'POST',
    dataType: 'json',
    data: object,
    cache: false,
    success: function () {

您的操作方式不对,请通过symfony路由发送:

url: "{{ path('save_sort_order') }}",

这应该是您所需要的全部