简体   繁体   English

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

[英]how to post with ajax to secured controller in Symfony2

I am trying to send an Json datatype to a secured controller (is_granted(ROLE_ADMIN)) , but, as you can guess, i can't get it to work, i get a 405 method not allowed response. 我正在尝试将Json数据类型发送到安全控制器(is_granted(ROLE_ADMIN)) ,但是,正如您可以猜测的那样,我无法使其正常工作,但我得到了405 method not allowed响应。

When i send it to an unsecured controller it works, so i have to let the controller know the post gets send with an admin role, but how? 当我将其发送到不安全的控制器时,它可以工作,因此我必须让控制器知道该帖子以管理员身份发送,但是如何?

This is my controller: 这是我的控制器:

/**
* @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);
    }

and this is my ajax post: 这是我的ajax帖子:

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

You are doing it the wrong way, send it via the symfony routing: 您做错了方法,请通过symfony路由发送:

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

this should be all you need 这应该是您所需要的

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

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