在使用 superagent-jsonp 跨域調用過程當中,直接報錯:Uncaught ReferenceError: superagentCallback*** is not definedjson
根本緣由是callback已經被瀏覽器回收了,經過設置timeout時間該問題能夠解決api
.use(jsonp({timeout: 10000}))
調用示例代碼以下:跨域
import request from 'superagent' import jsonp from 'superagent-jsonp' const state = { body: {} }; const mutations = { getMovies (state, response) { switch (response.tag) { case 'hotMovies': state.body = response; break; default: state.body = response; } } }; const actions = { getMovies ({ commit }, params) { request .get('https://api.douban.com/v2/movie/in_theaters?count=8') .accept('json') .use(jsonp({timeout: 10000})) .end((err, res) => { if (!err) { commit({ type: 'getMovies', tag: 'hotMovies', res: res.body }) console.log(res.body); } else { console.log(err); } }) } }; export default { state, mutations, actions }