突然之间,我在浏览器中的所有提取请求都会抛出无法在“窗口”无效模式下执行“提取”
。他们之前工作得很好。任何人都可以对此给出提示吗?我的代码如下:
function request(params) {
let { api, method, data } = params
let localParm = {
method: method,
mode: { mode: 'no-cors' },
credentials: 'include',
headers: typeof Headers !== 'undefined' ? new Headers({ 'content-type': 'application/x-www-form-urlencoded' }) : { 'content-type': 'application/x-www-form-urlencoded' }
}
let url = apiNames[api] || api
if (method == "POST") {
localParm.body = toQueryString(data)
} else if (method == "GET") {
if (data) {
url += ("?" + toQueryString(data || {}))
}
}
const reqFunc = method === 'jsonp' ? fetchJsonp : fetch;
return reqFunc(url, localParm).then((response) => {
if (response.status === 200 || response.ok === true) {
try {
return response.json()
}catch(e){
console.log('11')
}
} else {
console.log('22')
}
}).then((res) => {
return res
})
}
最佳答案
从 header 中删除 CORS 模式。看来您开始在 JavaScript 规范中使用 stage-0 或 stage-2 。在该规范中,出于某种原因,fetch 不允许 CORS 模式。
let localParm = {
method: method,
mode: { mode: 'no-cors' },
credentials: 'include',
headers: typeof Headers !== 'undefined' ? new Headers({ 'content-type': 'application/x-www-form-urlencoded' }) : { 'content-type': 'application/x-www-form-urlencoded' }
}
应该看起来像
let localParm = {
method: method,
credentials: 'include', ....
关于javascript - 无法在 'fetch' 无效模式上执行 'Window',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50808788/