export function jsonp(url) {
  if (url==null || !(url+"").trim()) {
    console.error('Axios.JSONP 至少需要一个url参数!')
    return;
  }
  url += ""

  return new Promise((resolve, reject) => {
    window.jsonCallBack = (result) => {
      resolve(result)
    }

    const headTag = document.getElementsByTagName("head")[0]
    const scriptTag = document.createElement("script");
    scriptTag.type = "text/javascript";
    scriptTag.src = url+(url.indexOf("?")>=0 ? "&": "?")+"callback=jsonCallBack"
    scriptTag.onerror = function(err){
      reject(err)
    }

    headTag.appendChild(scriptTag);

    setTimeout(() => {
      headTag.removeChild(scriptTag)
      reject("超时取消")
    }, 1000)
  })
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐