跨域请求JSONP

function getJSONP(url, callback) {
  var cbnum = "cb" + getJSONP.counter++
  var cbname = "getJSONP." + cbnum

  if(url.indexOf("?") === -1)
    url +=  "?jsonp=" + cbname
  else
    url += "&jsonp=" + cbname

  var script = document.creatElement("script")
  getJSONP[cbnum] = function(res) {
      try {
        callback(res)
      }
      finally {
        delete getJSONP[cbnum]
        script.parentNode.removeChild(script)  
      }
  }
  script.src = url
  document.body.appendChild(script)
}
getJSONP.counter = 0

你可能感兴趣的:(跨域请求JSONP)