Uncaught ReferenceError: jp2 is not defined,用jsonp抓取qq音乐总是说回调函数没有定义
問題如下參考鏈接:https://segmentfault.com/q/1010000010051040
用jsonp抓取qq音樂總是說回調(diào)函數(shù)沒有定義,
我的要實現(xiàn)時候的步驟
1。第一步
我要實現(xiàn)的目的
問題:如題
我的部分代碼:
import originJSONP from 'jsonp'export default function (url, data, option) {url += (url.indexOf('?') < 0 ? '?' : '&') + param(data)return new Promise((resolve, reject) => {originJSONP(url, option, (err, data) => {if (!err) {resolve(data)} else {reject(err)}})}) }// 將 data 拼接到 url 上 function param(data) {let url = ''for (let i in data) {let value = data[i] !== undefined ? data[i] : ''url += `&${i}=${encodeURIComponent(value)}`}return url }// 熱門歌單詳情歌曲 export function getHotSongList(disstid) {const url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'const data = Object.assign({}, commonParam, {type: 1,json: 1,utf8: 1,onlysong: 0,disstid,// jsonpCallback: 'playlistinfoCallback',loginUin: 0,hostUin: 0,inCharset: 'utf8',platform: 'yqq',needNewCode: 0})return jsonp(url, data, options) }// this.disc.dissid我已經(jīng)正確獲取了,通過自己服務(wù)端代理,代碼太多就不貼了。// TODO 報錯ReferenceError: jp1() is not definedgetHotSongList(this.disc.dissid).then((res) => {if (res.code === ERR_OK) {// 問題 不能打印出來console.log(res.cdlist[0].songlist)}})不知道為甚么,折騰一天了,還是沒解決,是QQ音樂的接口變了嗎,(前兩天同樣的代碼還能正常運(yùn)行的,昨天就不行了),希望有大神幫忙看看是怎么回事,先謝謝大家了。
補(bǔ)充
回調(diào)函數(shù)的名稱里邊處理好了,對其他接口(歌曲圖片,歌手,都是用jsonp獲取沒有問題),_jp1是一個默認(rèn)名稱而已。請求第二個它就變成_jp2,如此類推
處理的部分代碼
// 庫它自己定義的名字 var prefix = opts.prefix || '__jp';// use the callback name that was passed if one was provided.// otherwise generate a unique name by incrementing our counter.var id = opts.name || (prefix + (count++));該庫的地址:https://github.com/webmodules...
再次補(bǔ)充
修改options回調(diào)函數(shù)名字貌似不行,需要再次設(shè)置反向代理,即可解決問題,建議大家看一下MJingv的github相關(guān)反向代理設(shè)置,地址:https://github.com/MJingv/vue...
單純這個錯誤Uncaught ReferenceError: jp2 is not defined,
export function getSongList (disstid) {const url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'const data = Object.assign({}, commonParams, {disstid,type: 1,json: 1,utf8: 1,onlysong: 0,hostUin: 0,g_tk: 487535770,platform: 'yqq',needNewCode: 0})return jsonp(url, data, options) }利用如下方法解決
仔細(xì)閱讀一下第三方庫jsonp,里面的opts是一個對象,prefix屬性表示callback返回的名字,即傳參時候的val值,默認(rèn)是"__jp",param屬性表示的是傳參時候的key值。
修改方法,把options定義為:
export function getSongList (disstid) {const url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'const data = Object.assign({}, commonParams, {disstid,type: 1,json: 1,utf8: 1,onlysong: 0,hostUin: 0,g_tk: 487535770,platform: 'yqq',needNewCode: 0})const options = {param: 'jsonpCallback',prefix: 'playlistinfoCallback'}return jsonp(url, data, options) }這樣把問題解決了,但是還是獲取不到數(shù)據(jù)
確實不能通過jsonp的方式了,參照以前的接口,用node做轉(zhuǎn)發(fā)層。
app.get('/api/getSongList', function (req, res) {var url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'axios.get(url, {headers: {referer: 'https://y.qq.com/',host: 'c.y.qq.com'},params: req.query}).then((response) => {var ret = response.dataif (typeof ret === 'string') {// var reg = /^\w+\(({[^()]+})\)$/var reg = /{.*}/var matches = ret.match(reg) if (matches) {ret = JSON.parse(matches[0])}}res.json(ret)}).catch((e) => {console.log(e)})})在請求接口的地方用axios請求
export function getSongList(disstid) {const url = '/api/getSongList'const data = Object.assign({}, commonParams, {disstid,type: 1,json: 1,utf8: 1,onlysong: 0,platform: 'yqq',hostUin: 0,needNewCode: 0,g_tk: 67232076})return axios.get(url, {params: data}).then(res => {return Promise.resolve(res.data)}) }就可以獲取到了,songlist是放在獲取的結(jié)果中的
res.cdlist[0].songlist
總結(jié)
以上是生活随笔為你收集整理的Uncaught ReferenceError: jp2 is not defined,用jsonp抓取qq音乐总是说回调函数没有定义的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: U盟的错误详情分析
- 下一篇: 小米真机测试data文件夹看不见问题解决