美文网首页
跨域请求JSONP

跨域请求JSONP

作者: 猪丶八戒 | 来源:发表于2017-08-30 10:51 被阅读0次
    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

          本文链接:https://www.haomeiwen.com/subject/vyuzdxtx.html