【发布时间】:2021-06-07 23:07:50
【问题描述】:
我有一个将地图加载到页面上的功能,有 5% 的时间我遇到 403 错误并且地图数据无法正确加载。我很难触发错误,但我能够捕捉到它并截取屏幕截图(见下文)。
当页面刷新时,会发生加载正确数据的 GET 请求,然后发生加载正确地图的 POST 请求。有多个页面,每个页面都有自己的内容和地图,用户可以轻松地在页面之间导航。
即当 Mordor 页面加载时,Mordor 地图的数据会同时加载。当我导航到 Lothlorien 页面时,会加载 Lothlorien 地图的数据。
对造成这种情况的原因有什么想法吗?正如我所说,虽然这个错误很少发生,但我确实想深入了解它,以防止将来出现并发症。
注意事项:
-
我曾经遇到过频繁发生 403 错误的问题(如果页面空闲),但我通过
if resp.status === 403, window.location.reload解决了这个问题。这解决了问题,但我想知道我现在遇到的问题是否与它和“泄漏”有关,可以这么说。 -
webpack-internal出现在控制台中(见屏幕截图),但使用 webpack 搜索 403 错误却很短。 -
我在整个项目中使用
empty(),这样旧数据就不会保留在页面上。
async function loadMaps(){
let ret = axios.post( // ${location} is referenced at an earlier point, and always works
`${_Something}/something/_api/web/lists/getbytitle('Middle Earth Maps')/GetItems`,{
"query": {"__metadata": {"type":"SP.CamlQuery"}, "ViewXml": `<View><Query><Where><Eq><FieldRef Name='MiddleEarthMaps'/><Value Type='TaxonomyFieldType'>${location}</Value></Eq></Where></Query></View>`}
}, {
method: "POST",
credentials: "include",
mode: "no-cors",
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
}
}).then(resp => {
console.log(resp.status)
// in here there's other code that behaves as intended (95% of the time)
}).catch(err => {
if (err.status === 401 || err.status === 403) { // I use this for the main page of the project, and it works as intended
window.location.reload();
}
console.log("Error during loadMaps");
console.log(err);
return false;
});
return ret;
}
【问题讨论】:
标签: javascript sharepoint axios http-post http-status-code-403