使用jquery进行跨域请求,可在html
$.ajax({ success:function(data){ jquery
//若是返回json,data即为对象,jquery自动实现一个callback方法ajax
} })json
而在zepto中须要手动实现,由于zepto精简了不少东西跨域
下面附上代码服务器
function pandoraCall(data){ console.log(data); } //按需执行下面的跨域请求 $.ajax({ type:"get", url:"http://localhost/1.html?callback=?", dataType:"jsonp", success:function(data,status,xhr){ //这里的success方法是不会执行的,会调用上传pandoraCall方法,该方法名和服务器回传内容的方法名一致 console.log(data); console.log(status); console.log(xhr); }, error:function(e){ alert(e); } });
请注意,不能使用post请求,由于跨域请求是采用插入script标签的方式巧妙的获取数据而已,至关于插入js代码,因此传递的参数请跟在url后面post
简写jsonp
$.ajaxJSONP({
url:"http://localhost/1.html?callback=?"
});url
写问号也能够,默认调用的也是回传内容中的方法htm
OK,只会简单记录,若有疑问,请回复,谢谢