JSONP简单例子
var http = require('http'); var server = new http.Server(); server.listen(8000); server.on('request', function(request, response){ var url = require('url'); var params = url.parse(request.url, true).query; var fn = params.fn; response.writeHead(200, {'Content-type':'text/plain'}); response.write(fn+'(22)'); response.end(); });
前台
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script> var log = function(msg) { alert(msg); } </script> </head> <body> <script type="text/javascript"> function getJsonp(url, fn) { var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.onload = function() { head.removeChild(script); if (fn) { fn(); } } script.src = url; head.appendChild(script); } getJsonp("http://192.168.1.103:8000/?fn=log"); </script> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。