使用Symfony2进行跨域Ajax请求(使用FOSUserBundle的示例)

时间:2022-10-16 14:05:32

I have a domain A "xbo.dev" and a sub domain B "blog.xbo.dev".

我有一个域名“xbo.dev”和一个子域B“blog.xbo.dev”。

For example, on B domain, I would like to make an Ajax request to a domain A function.

例如,在B域上,我想向域A函数发出Ajax请求。

I'm trying this with FOSUserBundle authentication. What I do :

我正在尝试使用FOSUserBundle身份验证。我做的事 :

    var xhr = new XMLHttpRequest();

      if ('withCredentials' in xhr) {
          xhr.open('POST', 'http://xbo.dev/ajax/check_login_ajax', true);
          // I am using hard coding for the URL because with Routing.generate I would have this URL : http://blog.xbo.dev/ajax/check_login_ajax
          // which is not good because the request is on a different domain
          xhr.onreadystatechange = function() {
          if (xhr.readyState === 4) {
              if (xhr.status >= 200 && xhr.status < 400) {
                  console.log(xhr);
                  console.log('ok');
              } else {
                  console.log('ko');
              }
          }
      };
      xhr.send(encodeURI('_username=' + $('#co_'+varName+'username').val() + '&_password=' + $('#co_'+varName+'password').val() + '&_remember_me=' + false + '&_csrf_token=' + $('#co__csrf_token').val()));

I have a "ok" displayed by my request but nothing special is happening (I should have something like "Bad credentials" or "success" (or others), returned by FOSUserBundle check_login_ajax function, but no one of those is displayed...)

我的请求显示“ok”,但没有什么特别的事情发生(我应该有一些类似“坏凭据”或“成功”(或其他),由FOSUserBundle check_login_ajax函数返回,但没有显示任何一个... )

How can I do ?

我能怎么做 ?

EDIT 1 : Here is the result I have each time, either the login / password are correct or not

编辑1:这是我每次都有的结果,登录/密码是否正确

使用Symfony2进行跨域Ajax请求(使用FOSUserBundle的示例)

And with a normal $.ajax, I have this :

正常的$ .ajax,我有这个:

使用Symfony2进行跨域Ajax请求(使用FOSUserBundle的示例)

1 个解决方案

#1


1  

You need to implement something to relax the same-origin policy such as CORS.

您需要实现一些东西来放宽同源策略,例如CORS。

As to your in-code note about hard-coding vs using the router to generate URLs, Symfony's router supports hostnames.

至于关于硬编码和使用路由器生成URL的代码内注释,Symfony的路由器支持主机名。

#1


1  

You need to implement something to relax the same-origin policy such as CORS.

您需要实现一些东西来放宽同源策略,例如CORS。

As to your in-code note about hard-coding vs using the router to generate URLs, Symfony's router supports hostnames.

至于关于硬编码和使用路由器生成URL的代码内注释,Symfony的路由器支持主机名。