在使用 superagent-jsonp 跨域调用过程中,直接报错:Uncaught ReferenceError: superagentCallback*** is not defined
根本原因是callback已经被浏览器回收了,通过设置timeout时间该问题可以解决
.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
}
来源:oschina
链接:https://my.oschina.net/u/943746/blog/2051338