ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # 一 ## 1. 新建一个`js`文件夹 存放需要封装的内容 ## 2. 如建立一个 `http.js` 文件 // js/http.js ``` function http(url,callcake){ $.ajax({ type:"get", url, dataType:"jsonp", success(res){ callcake(res); var imgUrl = res.subjects[0].images.small; $("img").attr("src",imgUrl); } }) } ``` ## 3. 在html页面引入相关文件· ``` <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> <script src="js/http.js"></script> ``` ``` <img src="shuaige.jpg" alt=""> <script> var url = "https://douban.uieee.com/v2/movie/in_theaters" http(url,handleData); function handleData(res){ console.log(res); } </script> ``` # 对象封装 > 步骤应和`一`一样,此处有简化,不规范 ``` <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> ``` ``` <img src="shuaige.jpg" alt=""> <script> var url = "https://douban.uieee.com/v2/movie/in_theaters"; var obj ={ url, handleData:function(res){ console.log(res); } } http(obj); function http(obj){ $.ajax({ type:"get", url:obj.url, dataType:"jsonp", success(res){ obj.handleData(res); var imgUrl = res.subjects[0].images.small; $("img").attr("src",imgUrl); } }) } </script> ```