标签:blog http io ar os 使用 sp java for
<script>window.name={errno:0, errmsg:‘ok‘}</script>
<script>location.href=‘http://www.12306.cn/proxy.html?func=callback&errno=0&errmsg=ok‘</script>
<!doctype html> <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><script type="text/javascript"> (function(){ var queryStr = location.search.substring(1).split(‘&‘),oneQueryStr,args = {},g = parent,scope = parent ,callback; for(var i in queryStr){ oneQueryStr = queryStr[i].split(‘=‘); if(!callback && oneQueryStr[0] == ‘fun‘){ callback = oneQueryStr[1]; }; if(oneQueryStr[0]&&oneQueryStr[1]){ args[oneQueryStr[0]] = (oneQueryStr[1]||‘‘).replace(/[><‘"{}]/g, ‘‘); } } callback = callback.split(‘.‘); if( callback[0] === ‘document‘ || callback[0] === ‘location‘ || callback[0] === ‘alert‘){ }else{ for(var i = 0,len= callback.length;i<len;i++){ if(i==0 && callback[0]=="parent"){ g = parent; scope = parent; }else if(i==0 && callback[0]=="top"){ g = top; scope = top; }else{ if(i<len-1){ scope = scope[callback[i]]; } g = g[callback[i]]; } } g.call(scope,args); } })(); </script> </body></html>
$(function() { ‘use strict‘; /** * 交互类 * @param {object} param 要提交的数据 * @param {Object} [ajaxOpt] ajax配置 * @param {boolean} [https=false] 是否使用https协议 * @constructor */ var Sync = function(param, ajaxOpt, https) { var protocol = this.protocol = https ? ‘https‘: ‘http‘; var ajaxOptDefault = { url: protocol + ‘://‘+location.host, type: ‘GET‘, dataType: ‘jsonp‘, timeout: 20000 }; param = string2JSON(param) || {}; this.protocol = protocol; this.param = $.extend({}, param, ajaxOpt); this.ajaxOpt = $.extend({data: this.param}, ajaxOptDefault); this.HOST = protocol + ‘://‘+location.host; }; function string2JSON(str){ if($.isPlainObject(str)) { return str; } var params = {}; var str2Arr = str.split(‘&‘); $.each(str2Arr, function(i, keyVal) { var arr = keyVal.split(‘=‘); //去除serialize把空格转成+ params[arr[0]] = decodeURIComponent(arr[1]).replace(/[\+]+/, ‘‘).replace(/[\+]+/, ‘‘); }); return params; } $.extend(Sync.prototype, { /** * 通过get方式(jsonp)提交 * @param {String} [url] 请求链接 * @return {Object} promise对象 */ get: function(url) { var self = this; var send = $.ajax(url, this.ajaxOpt); return send.then(this.done, function(statues) { return self.fail(statues); }); }, /** * 通过post方式提交,依赖psp_jump.html文件 * @param {String} [url] 请求链接 * @return {Object} promise对象 */ post: function(url) { var deferred = $.Deferred(); var timer = null; var guid = parseInt(new Date().getTime().toString().substr(4), 10); var funName = ‘QBXJsonp‘ + guid; var formName = ‘QBXForm‘ + guid; var iframeName = ‘QBXIframe‘ + guid; // iframe 不能使用attr(‘name‘, xxx) 否则在ie7上添加的不是name属性而是submitName var iframe = $(‘<iframe name="‘ + iframeName +‘">‘).hide(); var param = $.extend({}, this.param, {proxy: this.HOST+‘/proxy.html‘, callback: funName}); var form = buildForm(param, {name: formName, target: iframeName, url: url || this.ajaxOpt.url}); window[funName] = function (data){ clearTimeout(timer); var value; for (var i in data) { if (data.hasOwnProperty(i)) { value = decodeURIComponent(data[i]); if (value.match(/^(\{.*\})|(\[.*\])$/)) { value = $.parseJSON(value); } data[i] = value; } } deferred.resolve(data); }; timer = setTimeout(function(){ deferred.reject({ errno: 999999, errmsg: ‘网络超时,请稍后重试‘ }); }, this.ajaxOpt.timeout); // do some clear deferred.always(function(data) { $(iframe).remove(); $(form).remove(); // IE8以下不支持delete window属性 try { delete window[funName]; } catch (e){ window[funName] = null; } }); $(document.body).append(iframe).append(form); $(form).submit(); return deferred.then(this.done); }, /** * 收到响应时默认回调 * @param {Object} data 数据 * @return {Object} */ done: function (data) { var deferred = $.Deferred(); if (data.errno == 0) { deferred.resolve(data); } else { deferred.reject(data); } return deferred.promise(); }, /** * 未收到响应时默认回调 * @param {Object} error 错误信息 * @return {Object} */ fail: function(error) { var deferred = $.Deferred(); deferred.reject({ errno: 999999, errmsg: ‘网络超时,请稍后重试‘ }); return deferred.promise(); } }); /** * 把数据对象转成form元素 * @param {Object} data json数据 * @param {Object} opts form所需参数或其他数据 * @return {Object} */ function buildForm(data, opts) { if (opts.url) { opts.action = opts.url; delete opts.url; } opts.method = ‘post‘; var $form = $(‘<form>‘).attr(opts).hide(); $.each(data, function (name, value) { $(‘<input>‘).attr({type: ‘hidden‘, name: name, value: value}).appendTo($form); }); return $form[0]; } window.Sync = Sync; });
var login = function(data) { var sync = new Sync(data); return sync.post(sync.HOST+‘/login‘); } login({username: ‘blackMao‘, password: ‘blackMao‘}) .done(function() { alert(‘登陆成功!‘); }) .fail(function(error) { alert(error.errmsg); });
至此就可以做到跨域请求了~
标签:blog http io ar os 使用 sp java for
原文地址:http://www.cnblogs.com/xiaoheimiaoer/p/4101588.html