关于http请求:request与superagent,用request能请求到数据,用superagent不能请求到,求大神指点迷津,谢谢
发布于 9 天前 作者 jaxQin 363 次浏览 来自 问答
    const url='http://h5vv.video.qq.com/getinfo?callback=txplayerJsonpCallBack_getinfo_591513&isHLS=false&charge=0&vid=y00221a60w7&defn=hd&defnpayver=1&otype=json&guid=29a06bf3852fbe2ea6eb53829c3878fa&platform=10901&sdtfrom=v1010&host=v.qq.com&_rnd=1479010822&fhdswitch=0&show1080p=1&_qv_rmt=sNk0sWZTA17002uQa%3D&_qv_rmt2=0Qs65I9%2B149182HOQ%3D&_=1479010820769'
  
    // request.get(url,(err,sres,body)=>{
    //     res.send({'ss':body})
    // })
    superagent.get(url).end((err,sres)=>{
        res.send({'qq':err,'ww':sres})
    })

求指点,谢谢

2 回复

jsonp跨域

  1. npm i superagent-jsonp
  2. import jsonp from ‘superagent-jsonp’ or const jsonp = require(‘superagent-jsonp’)
const url='http://h5vv.video.qq.com/getinfo?callback=txplayerJsonpCallBack_getinfo_591513&isHLS=false&charge=0&vid=y00221a60w7&defn=hd&defnpayver=1&otype=json&guid=29a06bf3852fbe2ea6eb53829c3878fa&platform=10901&sdtfrom=v1010&host=v.qq.com&_rnd=1479010822&fhdswitch=0&show1080p=1&_qv_rmt=sNk0sWZTA17002uQa%3D&_qv_rmt2=0Qs65I9%2B149182HOQ%3D&_=1479010820769'
// 方法1:
superagent
	.get(url)
	.use(jsonp({
		callbackName: 'txplayerJsonpCallBack_getinfo_591513'
	})
	.then(res => {
		// codeing...
	})
// 方法2:	
// 地址中的`callback=txplayerJsonpCallBack_getinfo_591513`移除
superagent
	.get(url)
	.use(jsonp(false))
	.then(res => {
		// codeing...
	})

谢谢

回到顶部