function JSONPhandle(d){ console.log(d); }; var Script=document.createElement('script'); Script.src='http://localhost:3000/jsonp?cb=JSONPhandle'; document.body.append(Script);
router.get('/jsonp', function(req, res, next) { var msg=JSON.stringify('老铁,JSONP了解一下') if(req.query.cb){ var str=req.query.cb+'('+msg+')'; res.send(str); return; } res.send(msg); });
普通跨域请求:只服务端设置Access-Control-Allow-Origin即可,前端无须设置
<script> $.get('http://localhost:3000/cors', (data) => { console.log(data); }) </script>
var createError = require('http-errors'); var express = require('express'); var cors = require('cors'); var app = express(); app.use(cors());