标签:style http io java ar 数据 div sp cti
jsonp的原理很简单,主要利用了HTML中所有有src的属性的标签可以跨域的特点,利用script的src进行get请求,后端输出一段js代码的字符串在script中便会执行。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>
标签:style http io java ar 数据 div sp cti
原文地址:http://blog.csdn.net/songzheng_741/article/details/39449587