比如以下代码:
var res = fetch('/api/blog/detail/' + BlogGlobals.blogID).then(res => res.json())
console.log(res);
可以返回正常结果,但是:
function clickBotton(){
var res = fetch('/api/blog/detail/' + BlogGlobals.blogID).then(res => res.json())
console.log(res);
};
会报错
Uncaught (in promise) TypeError: Failed to fetch
at HTMLButtonElement.clickBotton (userscript.html?id=f905148f-9aca-4422-95a1-bff3314b907d:27:19)
clickBotton @ userscript.html?id=f905148f-9aca-4422-95a1-bff3314b907d:27
Promise.then(异步)
clickBotton