Javascript Axios发出的Drupal和CORS请求:无法检查登录状态

Javascript Axios发出的Drupal和CORS请求:无法检查登录状态,javascript,vuejs2,cors,axios,drupal-8,Javascript,Vuejs2,Cors,Axios,Drupal 8,我正在创建独立的html/javascript应用程序,它应该与Drupal8.6.2的RESTAPI通信。我的应用程序使用Vue.js,因此ajax请求由Axios发送 我希望能够在同一个域上运行应用程序,并通过CORS从另一个域运行应用程序。假设我的Drupal坐在 我可以使用以下代码从和从中登录和注销: //Login axios.post( "https://drupal8.example.org/user/login?_format=json", JSON.stringify(

我正在创建独立的html/javascript应用程序,它应该与Drupal8.6.2的RESTAPI通信。我的应用程序使用Vue.js,因此ajax请求由Axios发送

我希望能够在同一个域上运行应用程序,并通过CORS从另一个域运行应用程序。假设我的Drupal坐在

我可以使用以下代码从和从中登录和注销:

//Login
axios.post(
  "https://drupal8.example.org/user/login?_format=json",
  JSON.stringify({
    'name': "myusername",
    'pass': "mypassword"
}), {
  headers: {
    'Content-Type': 'application/hal+json'
  },
  withCredentials: true
}).
then(function (response) {
  //my code goes here
});

// logout
axios.post(
  "https://drupal8.example.org/user/logout?_format=json&token=" + logout_token, {}, {
    headers: {
      'Content-Type': 'application/hal+json'
    },
    withCredentials: true
  }
).
then(function () {
  //my code goes here
})
但是我完全无法通过CORS检查用户是否登录:

//check login status
axios.get(
  "https://drupal8.example.org/user/login_status?_format=json", {}, {
    headers: {
      'crossDomain': true,
      'Content-Type': 'application/hal+json'
    },
    withCredentials: true
  }
).then(function (response) {
  console.log(response.data); // 1 from the same domain, 0 from localhost; should be 1 in both cases
});
如果我在浏览器窗口中检查url,它会显示1,但CORS发出的请求返回0


问题的原因是什么以及如何解决?

好吧,解决方法很简单,但原因我还不知道。我必须为Axios设置默认选项

axios.defaults.withCredentials = true;
显然,
default.withCredentials
withCredentials
随单个请求发送之间有很大的区别。我不知道这是一个错误还是一个功能


帮助我找到解决方案。

Drupal服务器响应是否包含访问控制允许凭据响应头?确实:
访问控制允许凭据:true