解决使用ajax时session无效的问题

问题:node使用express-session,ajax,后台路由获取不到session!
解决方案:ajax默认不携带cookie,要想携带cookie或者session,需要设置credentials(fetch) / withCredentials(XMLHttpRequest)。

如果使用的是fetch,方案如下:

fetch('url', {credentials:'include'});
//或者
fetch("url", {
        method: "POST",
        credentials: "include",
        body: params,
        headers: {
          "Content-Type": "application/json"
        }
      })



如果使用的是XMLHttpRequest,解决方案如下:

var xhr = new XMLHttpRequest();
hhr.withCredentials = true;

猜你喜欢

转载自blog.csdn.net/chaoyangsun/article/details/79648055