前端MVC框架[01] 发送JSONP请求
默认分类 2012-10-11 07:37:50
-
-
-
-
-
-
-
-
-
-
- Requester = {};
-
-
-
- Requester.proxy = {};
- Requester.JSONP = function (url, data, onsuccess, action) {
- var me = this,
-
- proxy = me.getValidProxy(action);
-
- proxy['action'] = action;
- proxy['onsuccess'] = onsuccess;
- proxy['status'] = 'send';
- document.getElementById(proxy['id']).src = url + '?rand='+Math.random()+'&callback=Requester.proxy["'+proxy['id']+'"].callback';
- };
-
-
-
-
-
-
- Requester.getValidProxy = function() {
- var me = this,
- i,
- proxy = null,
- script;
-
-
- for (i in me.proxy) {
- if (i && me.proxy[i] && me.proxy[i].status == 'finished') {
- script = document.getElementById(i);
- if (script && window.addEventListener) {
- script.parentNode.removeChild(script);
- proxy = me.createProxy(i);
- }
- break;
- }
- }
- proxy = proxy || me.createProxy();
- me.proxy[proxy.id] = proxy;
-
- return proxy;
- };
-
-
-
-
-
-
- Requester.createProxy = function(id){
- var me = this,
- proxy = {};
-
- proxy.id = id || (new Date()).getTime() + '' + Math.random();
- proxy.status = 'finished';
- proxy.callback = me.creatProxyCallback();
-
- var script = document.createElement('script');
- script.id = proxy.id;
- script.type = 'text/javascript';
- script.charset = 'utf-8';
- document.getElementsByTagName('head')[0].appendChild(script);
- script = null;
-
- return proxy;
- };
-
-
-
-
-
- Requester.creatProxyCallback = function(){
- return function(data) {
-
- var proxy = this,
- errorMap,
- key,
- input,
- formMap = {};
-
- proxy.status="finished";
-
-
- if (data && proxy.action && String(data.success).replace(/\s/ig,'').toLowerCase() !== 'true' ) {
- if (Requester.backendError) {
- Requester.backendError(data);
- }
- }
-
-
- if (proxy.onsuccess) {
- proxy.onsuccess(data);
- }
- }
- };
-
- Requester.JSONP('http://www.5imemo.com/other/ajax/jsonp.php','',function(data){
- alert(data.id)
- });
>>
留言评论