0510日重点:原生js修改豆瓣电影api 在angularjs里运用出现的bug
用$http.jsonp调用豆瓣电影api,会出现返回数据格式错误的bug。在控制器里加上下面的代码,才能正常获取到数据。
function jsonp(url, callback) {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = url + (url.indexOf('?') > 0 ? '&' : '?') + 'callback=CB&' + Date.now();
script.onload=function(){
script.parentNode.removeChild(script);
};
window['CB'] = function (json) {
callback(json);
};
document.head.appendChild(script);
}
jsonp('
https://api.douban.com/v2/movie/coming_soon',function(data
){
console.log(data);
});
var script = document.createElement('script');
script.type = "text/javascript";
script.src = url + (url.indexOf('?') > 0 ? '&' : '?') + 'callback=CB&' + Date.now();
script.onload=function(){
script.parentNode.removeChild(script);
};
window['CB'] = function (json) {
callback(json);
};
document.head.appendChild(script);
}
jsonp('
){
console.log(data);
});
浙公网安备 33010602011771号