9.5 jsonp 实现


server
const http = require('http') const url = require('url') http.createServer((req,res) => { let msg = '服务端消息' res.writeHead(200,{"Content-Type": "text/plain;charset=utf-8"}) //拿到请求数据 let URL = url.parse(req.url,true) let callback = URL.query.callback //console.log(callback) res.write(`${callback}("${msg}")`) res.end() }).listen(3000, () => { console.log('开启服务端') })

  

user-agent

<button class="btn">发送</button>
  <script>
    function show(params) { 
      alert(params+'拿到了')
      let sc = document.querySelector('script:last-child')
      //删除新加的
      document.getElementsByTagName('body')[0].removeChild(sc)
      
    }
    let btn = document.getElementsByClassName('btn')[0]
    btn.addEventListener('click',() => {
    let sc = document.createElement('script')
    sc.src = `http://localhost:3000?callback=show`
    document.getElementsByTagName('body')[0].appendChild(sc)
    
    //console.log( document.getElementsByTagName('body'),sc)
   })
  </script>

  

posted @ 2020-09-05 16:03  尽世间恶丑  阅读(143)  评论(0编辑  收藏  举报