Skip to content

Instantly share code, notes, and snippets.

@wulucxy
Created July 9, 2018 01:20
Show Gist options
  • Save wulucxy/68867b7eb3a20c2a5d66256e9454aecd to your computer and use it in GitHub Desktop.
Save wulucxy/68867b7eb3a20c2a5d66256e9454aecd to your computer and use it in GitHub Desktop.
Http #snippet

Http

getHeader

获取头字段 大小写不敏感

const getHeader = (header, field) => {
  if (!header) {
    return ''
  }

  const name = Object.keys(header).find(f => compareStrings(f, field, true))
  return header[name] || ''
}

Demo:

var header = {
  'Content-Type': 'application/json',
  'x-requested-with': 'XMLHttpRequest'  
}

getHeader(header, 'content-type') // 'application/json'

isAjax

判断是否是异步请求

const isAjax = (header) => {
  return getHeader(header, 'x-requested-with') === 'XMLHttpRequest'
}

Demo:

var header = {
  'Content-Type': 'application/json',
  'x-requested-with': 'XMLHttpRequest'  
}

isAjax(header) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment