读书人

一个容易的JSONP的测试例子

发布时间: 2012-11-03 10:57:43 作者: rapoo

一个简单的JSONP的测试例子
转自http://pldream.com/b/?post=71
首先准备两个域,在本地配置两个域,用来测试,这里配置为http://p.cn 和 http://as.cn
在p.cn下准备客户端代码,在as.cn下准备服务端提供的接口
1 提供客户端发送请求的方法

function callJSONPServer (url) {//提供客户端发送请求的方法oscript = document.getElementById(url);if (oscript) {document.body.removeChild(oscript);} var script = document.createElement('script');script.setAttribute('type', 'text/javascript');script.setAttribute('src', url);script.setAttribute('id', url);document.body.appendChild(script);}

2 在服务端(跨域的情况下),提供的接口方法
function onJSONPServerResponse () {alert(new Date().getTime());}eval('(' + onJSONPServerResponse() + ')');

3 在客户端简单测试

读书人网 >JavaScript

热点推荐