javascript - 无法在 'fetch' 无效模式上执行 'Window'

标签 javascript fetch

突然之间,我在浏览器中的所有提取请求都会抛出无法在“窗口”无效模式下执行“提取”。他们之前工作得很好。任何人都可以对此给出提示吗?我的代码如下:

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/

相关文章:

ios - 仅当应用程序处于事件状态时,如何在 performFetchWithCompletionHandler 中执行代码?

javascript - 打开chrome ://URL in a new tab from bookmark

javascript - Highcharts : Disable top spacing from column graph

database - 从 Magento 数据库获取查询——mysql_num_rows

arrays - 利用检索到的对象的属性来填充数组和后续标签的最佳方法?

ios - 向使用 Fetch Controller 的 TableView 添加编辑行操作?

javascript - 使用 PostgreSQL 选择查询的 Knex 在多个并行请求上性能极度下降

javascript - 尝试基于 JSON 类型显示内容,没有带 Angular Controller

javascript - Konva 文本边界

python - 使用python多线程下载循环