duanfen7676 2015-11-10 10:29
浏览 52

带有子域Symfony2的Ajax请求(FOSUserBundle示例)

I have a main domain, site.dev and a subdomain, sub.site.dev.

I have a working connection request with FOSUserBundle, from site.dev. Now, I want to allowed this request from the subdomain sub.site.dev too.

When I try to do that, I have an error in the debug console : XMLHttpRequest cannot load http://site.dev/ajax/check_login_ajax. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://sub.site.dev' is therefore not allowed access.

Here is the part of my security.yml file :

firewalls:
    main:
        pattern: ^/
        form_login:
            login_path: fos_user_registration_register

            check_path:      check_login_ajax
            success_handler: user.security.authentication_handler
            failure_handler: user.security.authentication_handler
            provider: fos_userbundle
            csrf_provider: security.csrf.token_manager
        logout:
            path:   fos_user_security_logout
            target: /
        anonymous:    true

I already looked the "host" parameter in routing files, but can't find anything working well for both domain. I thought about 2 firewalls, but it doesn't work too.

Thanks a lot.

EDIT : Here, I'm talking about FOSUserBundle routing and Ajax, but I want this solution works for all the ajax requests I choose.

EDIT 2 : When I execute the AJAX request you can see under, I have the .fail code executed :

$.ajax({
        type: "POST",
        url: myRoute,
        dataType: 'json',
        data: {
            _username: $('#co_username').val(),
            _password: $('#co_password').val(),
            _remember_me: false,
            _csrf_token: $('#co__csrf_token').val()
        }
    }).done(function (data) {
        console.log('1');
    }).fail(function (data) {
        console.log('2');
    });

EDIT 3 : I just figured that an AJAX request is performed but nothing appears in the Symfony2 debug toolbar. So, I executed this code, to try :

$.ajax({
        type: "POST",
        url: Routing.generate('check_login_ajax'),
        dataType: 'json',
        data: $('#connection-form').serialize(),
        success: function(data) {
            console.log('3');
        }
    }).done(function (data) {
        console.log('1');
    }).fail(function (data) {
        console.log('2');
    });

'2' is still printed but I'm connected afterwards though.

How should work the ajaxSubmit with my code ? Do I have to include an extern library of jQuery ? And what's really the point of it ? Not sure to get everything

EDIT 4 : the statusText of my query is "OK" and the status is "200", so, why the .fail code is it executed... ?

EDIT 5 : To really understand the situation. I'm redirected to inscription/ when I try to reach check_login_ajax. Here is a preview of my debug developer toolbar

debug developer toolbar

As you can see in my firewall, a login_path is set login_path: fos_user_registration_register. fos_user_registration_register being the inscription/ route.

When I execute the AJAX request which is in EDIT 3, I have the results :

200
OK
2
  • 写回答

1条回答 默认 最新

  • doupu1949 2015-11-10 11:17
    关注

    Add this code in your .htaccess under web folder:

    <IfModule mod_headers.c>
        Header add Access-Control-Allow-Origin "http://sub.site.dev"
    </IfModule>
    

    don't forget to change "http://sub.site.dev" with your real subdomain.

    For EDIT 2

    Try to implement AJAX like this :

    $.ajax({
               type: "POST",
               url: "{{path('check_login_ajax')}}",
               data: $("#idForm").serialize(), // serializes the form's elements.
               success: function(data)
               {
    
               }
           });
    

    Also you can use AjaxSubmit

    $(this).ajaxSubmit({
          url: "{{path('check_login_ajax')}}",
          type: "POST",
          success: function (result){
    
          }
    )};
    

    I hope this help.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么