1.一个封装的例子
$$ = function(win){
var isIE = !-[1,];
doc = win.document;
head = doc.getElementsByTagName('head')[0];
function request(url, success, timestamp){
var script = doc.createElement('script');
function callback(){
if(typeof jsonp != 'undefined'){
success(jsonp);
}else{
//success("");
//alert('warning: jsonp did not return.');
alert("Shopalと通信ができません");
}
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
if( head && script.parentNode ){
head.removeChild(script);
}
}
if(isIE){
script.onreadystatechange = function(){
var readyState = this.readyState;
if(readyState == 'loaded' || readyState == 'complete'){
callback();
}
};
}else{
script.onload = function(){
callback();
};
}
if(timestamp){
url += '?ts=' + (new Date).getTime();
}
script.src = url;
head.insertBefore(script, head.firstChild);
}
return {load:request};
}(this);
----------------------------
原理:
<script type="text/javascript">
// This is our function to be called with JSON data
function showPrice(data) {
alert("Symbol: " + data.symbol + ", Price: " + data.price);
}
var url = “ticker.js”; // URL of the external script
// this shows dynamic script insertion
var script = document.createElement('script');
script.setAttribute('src', url);
// load the script
document.getElementsByTagName('head')[0].appendChild(script);
</script>
http://club.topsage.com/thread-327033-1-1.html
分享到:
相关推荐
下面阐述一下AngularJS中使用$http实现跨域请求数据。 AngularJS XMLHttpRequest:$http用于读取远程服务器的数据 $http.post(url, data, [config]).success(function(){ ... }); $http.get(url, [config]).success...
JSONP实现跨域 常用的jquery实现跨域调用 $.ajax({ url: "http://127.0.0.1/~chenjiebin/mycode/php/crossdomain/index.php", dataType: "jsonp", jsonp: "callback", context: document.body, success: ...
基于jQuery的jsonp ajax跨域请求,
什么是跨域请求? 简单的理解就是向不在同一个域名的服务器文件发出请求。这个还是用实际的例子来说明一下吧,比如baidu.com向cxyblog.com发送请求,这两个域名是不同的,那么这就是跨域了,出于安全性的考虑,这样...
一个ajax跨域请求的demo实例,前端采用jquery的ajax,后端采用tomcat集成的HttpServlet的方法接受前端的数据和返回数据
cross-request 3.1 YApi 跨域请求 谷歌浏览器
快速搭建跨域请求的jar包,帮助前后端快速解锁,分离。
本篇文章主要介绍了Vuejs2.0 如何利用proxyTable实现跨域请求,具有一定的参考价值,有兴趣的可以了解一下
通过jquery的ajax处理跨域请求的DEMO,代码比较简单,仅供学习参考。
一、原生JS实现ajax 第一步获得XMLHttpRequest对象 第二步:设置状态监听函数 第三步:open一个连接,true是异步请求 第四部:send一个请求,可以发送一个对象和字符串,不需要传递数据发送null 第五步:在监听函数...
跨域的方法很多,通常都需要后台配置,不过vue-cli创建的项目,可以直接利用node.js代理服务器,通过修改vue proxyTable接口实现跨域请求。在vue-cli项目中的config文件夹下的index.js配置文件中,修改前的dev: ...
spring boot 支持跨域 前台不需要jsonp 请求 正常js即可 spring boot 支持跨域 前台不需要jsonp 请求 正常js即可
主要介绍了js实现跨域访问的三种方法,包括基于iframe实现跨域、基于script标签实现跨域以及后台代理方式 ,感兴趣的小伙伴们可以参考一下
JS跨域请求解决方案.docx
虽然有同源策略的存在,但是在js中跨域也依然很常见,有document.domain、window.name、图片ping、jsonp、CORS,在这里简单总结下图片ping、jsonp和CORS备忘。 图片ping 图片可以从任何URL中加载,所以将img的src...
解决sockJs实现的websocket的跨域问题,后台使用的springboot框架,maven
由于js同源策略的影响,当在某一域名下请求其他域名,或者同一域名,不同端口下的url时,就会变成不被允许的跨域请求。 那这个时候通常怎么解决呢,对此菜鸟光头我稍作了整理: 1.JavaScript 在原生js(没有jQuery...
跨域请求类
javascript跨域请求包装函数与用法示例.docx