In the development process, you may encounter multiple JSONP requests with the same callback function name. In this case, you need to package a JSONP function. Here is a simple implementation:

function jsonp(url, jsonpCallback, success) {
  let script = document.createElement('script')
  script.src = url
  script.async = true
  script.type = 'text/javascript'
  window[jsonpCallback] = function(data) {
    success && success(data)
  }
  document.body.appendChild(script)
}
jsonp('http://xxx'.'callback'.function(value) {
  console.log(value)
})
Copy the code