跨域相关的文章老早写过几篇:
jquery url检测遇到的jquery跨域问题及JSONP的使用
本文是对第一篇的实例补充。
js脚本
$.getJSON ('http://sjolzy.cn/?callback=? &a=1&b=2', function(data) {
if (typeof(data) == 'object') {
$.each(data,function(i,j){
// ...
});
}
});
服务端代码
跨域相关的文章老早写过几篇:
jquery url检测遇到的jquery跨域问题及JSONP的使用
本文是对第一篇的实例补充。
js脚本
$.getJSON ('http://sjolzy.cn/?callback=? &a=1&b=2', function(data) {
if (typeof(data) == 'object') {
$.each(data,function(i,j){
// ...
});
}
});
服务端代码
先提下关于jQuery的$.Ajax 的async的作用,
官方的解释是
async Boolean Default: true
By default, all requests are sent asynchronous (e.g. this is set to true by default). If you need synchronous requests, set this option to false. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.
async默认是true, 即为异步方式, $.Ajax执行后, 会继续执行ajax后面的脚步, 直到服务器端返回数据后, 触发$.Ajax里的success方法. 这时候执行的是两个线程.
我的出现闪屏的情况是:
$.ajax({
type: "post",
url: "index.php",
data: { },
async:false,
[代码] [JavaScript]代码
var Ajax={};
Ajax._xmlHttp = function(){ return new (window.ActiveXObject||window.XMLHttpRequest)("Microsoft.XMLHTTP");}
Ajax._AddEventToXHP = function(xhp,fun,isxml){
xhp.onreadystatechange=function(){
if(xhp.readyState==4&&xhp.status==200)
fun(isxml?xhp.responseXML:xhp.responseText);
}
}
Ajax.get=function(url,fun,isxml,bool){
var _xhp = this._xmlHttp();
this._AddEventToXHP(_xhp, fun || function(){} ,isxml);
_xhp.open("GET",url,bool);
_xhp.send(null);
}
Magento默认采用的prototype库,遇到关于prototype的document.observe('dom:loaded', function() {}); 的用法在IE不起作用的问题(在火狐下工作OK)。
document.observe('dom:loaded', function() {
alert('dom:loaded');
});
最后暂时解决这个兼容问题的处理是:
添加浏览器判断,在IE浏览器下改用Event.observe(window, 'load', function() {});
js判断浏览器 :
var Browser = {
'isIE' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0),
jQuery使用得比较顺手也比较喜欢,不得已也要用Prototype,小小整理下区别。。
// JQuery
// JQuery Shorthand
// Prototype