Home >Web Front-end >JS Tutorial >How does the node server achieve the acquisition of Douban data (code)
The content of this article is about how the node server implements the acquisition of Douban data (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script> </head> <body> <input type="text" class="ipt" /> <button>获取数据</button> <ul> </ul> </body> <script type="text/javascript"> // $('button').click(function(){ // $.ajax({ // type:"post", // url:"http://localhost:3000", // data:{name:$('.ipt').val()}, // success:function(data){ // console.log(data) // } // }); // }) $('button').click(function(){ $.ajax({ type:"get", url:"http://localhost:3000", dataType:'json', success:function(data){ console.log(data) data.map(function(item){ $('ul').append('<li>'+item+'</li>') }) } }); }) </script> </html>
const https=require('https'); var http=require('http') const options = { hostname: 'api.douban.com', port: 443, path: '/v2/movie/top250', method: 'GET' }; var str='' var arr=[] //发起请求 const req = https.request(options, (res) => { res.on('data', (d) => { // console.log(d.toString()) str+=d; }); res.on('end',()=>{ var list=JSON.parse(str).subjects list.map(function(item){ arr.push(item.title) }) }) }); http.createServer((req,res)=>{ res.writeHead(200,{'content-type':'text/html;charset=utf-8','Access-Control-Allow-Origin':'*'}) if(req.url!='/favicon.ico'){ res.write(JSON.stringify(arr)) res.end() } }).listen(3000) //请求失败 req.on('error', (e) => { console.error(e); }); //结束请求 req.end();
Recommended related articles:
Communication between Vue child components and parent components (with code)
v-model implementation What is the principle? Introduction to how to use v-model (with code)
The above is the detailed content of How does the node server achieve the acquisition of Douban data (code). For more information, please follow other related articles on the PHP Chinese website!