原生koa2实现jsonp
123456789101112131415161718192021222324252627282930
const koa = require('koa');const app = new koa();app.use(async (ctx) => { // 判断是否为jsonp的请求 if (ctx.method === 'GET' && ctx.url.split('?')[0] === '/getData.jsonp') { // 获取jsonp的callback let callbackName = ctx.query.callback || 'callback'; let returnData = { success: true, data: { text: 'this is a jsonp api', time: new Date().getTime() } } // jsonp的script字符串 let jsonpStr = `;${callbackName}(${JSON.stringify(returnData)})`; // 用text/javascript,让请求支持跨域获取 ctx.type = 'text/javascript'; // 输出jsonp字符串 ctx.body = jsonpStr } else { ctx.body = 'hello jsonp'; }});app.listen(3004, () => { console.log('[demo] jsonp is starting at port 3004')});
koa-jsonp中间件实现
12345678910111213141516171819202122
const koa = require('koa');const jsonp = require('koa-jsonp');const app = new koa();app.use(jsonp());app.use(async (ctx) => { let returnData = { success: true, data: { text: 'this is a jsonp api', time: new Date().getTime(), } }; // 直接输出json ctx.body = returnData;});app.listen(3004, () => { console.log('[demo] jsonp is starting at port 3004')});