原生js写法
var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://localhost:7001/api/userinfo', true); xhr.withCredentials = true; // 开启withCredentials xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { console.log("请求登录状态",xhr.responseText); } };
xhr.send();
axios写法
axios({ method: 'get', url: 'http://localhost:7001/api/userinfo', withCredentials: true, // 跨域请求时发送Cookie }) .then(function (response) { console.log("请求登录状态",response); });fetch写法:(fetch要使用credentials)
fetch('http://localhost:7001/api/userinfo',{ method: 'GET', credentials:"include" }).then((response)=>response.json(),(error)=>{ //处理错误 }).then(json=>{ console.log("请求登录状态",json); })
标签:axios,跨域,fetch,xhr,api,cookie,写法,response From: https://www.cnblogs.com/fhysy/p/17577756.html