ajaxドメイン間解決方法

5087 ワード

ドメイン間POST
(function($){
	window.isIE6=$.browser.msie && jQuery.browser.version==6.0;
	jQuery.extend({
		ajaxFormNums:0,
		ajaxFormPost:function(sURL, datas, callBack,domains){/*[sURL=    , datas=        , callBack= 

 ,domain= ]*/
			domains=domains||'51.com';
			var on='TEMP_POST_'+$.ajaxFormNums;
			var of=[];
			of.push('<div id="'+on+'_DIV" style="position:absolute;z-index:10;top:-20000100px;"><iframe 

id="'+on+'_IFRAME" name="'+on+'_IFRAME" height="1" width="1" src="http://friend.51.com/ajax_blank.php?d='+encodeURIComponent

(domains)+'" frameborder="0" border="0" scrolling="no"></iframe><form id="'+on+'_FORM" name="'+on+'_FORM" method="post" 

action="'+sURL+'" target="'+on+'_IFRAME">');
			$.each(datas,function(i,n){of.push('<textarea name="'+i+'" 

style="width:1px;height:1px;">'+n+'</textarea>');});
			of.push('<input type="submit" value="Submit" name="b1" style="width:1px;height:1px;" /></form></div>');
			$(document.body).append(of.join(''))//.insertAdjacentHTML("beforeEnd", of.join(''));
			//document.body.insertAdjacentHTML("beforeEnd", of.join(''));
			of=null;
			$('#'+on+'_IFRAME').bind('load',function(){
				if(!$(this).attr('summary'))
				{
					$(this).attr('summary',1);
					$('#'+on+'_FORM').submit();
					return false;
				}

				if(isIE6)
				{
					if($.isFunction(callBack))
					{
						callBack(window.name);
					}
					else
					{
						eval(callBack+'(window.name)');
					}
				}
				else
				{
					var oIf= this.id;
					if($.isFunction(callBack))
					{
						if(navigator.userAgent.toLowerCase().indexOf('se 2.x')>-1)
						{
							callBack(frames[oIf].document.body.innerText);
						}
						else
						{
							callBack(frames[oIf].document.body.innerHTML);
						}
					}
					else
					{
						eval(callBack+'(frames[oIf].document.body.innerHTML)');
					}
				}
				window.setTimeout(function(){$('#'+on+'_DIV').remove();},1);
			});
			$.ajaxFormNums++;
		}
	});
})(jQuery);

呼び出し方法:
$.ajaxFormPost('http://localhost/api.php?act=say', {cont:cont}, function(data){
alert(data);
});

Webサイトのルートディレクトリにファイルajax_を作成blank.php
内容は次のとおりです.
<html><head><title>51.com</title><script type="text/javascript">document.domain="51.com";</script></head><body></body></html>

呼び出したページにjsを追加します.
document.domain='51.com';

php部
$result=-1;
echo "<script>if(!/msie 6/i.test(navigator.userAgent)){document.domain='51.com';}else{parent.name='$result';}</script>$result";

ドメイン間GET
$.getJSON("http://localhost/api.php?callback=?",{receiver:receiver},function(data){
alert(data.info);
})

php処理部:
$ret['info'] = iconv("GBK", "UTF-8", "      ");
$result = json_encode($ret);
echo request_check($_GET['callback']).'('.$result.')';
$_GET['callback']     ,  rss  
function request_check($post){    
    if(!get_magic_quotes_gpc())    //   magic_quotes_gpc         
    {
        $post = addslashes($post);    //   magic_quotes_gpc                   
    }    
    //$post = str_replace("_", "\_", $post);    //   '_'       
    //$post = str_replace("%", "\%", $post);    //  ' % '       
    $post = nl2br($post);    //         
    $post= htmlspecialchars($post, ENT_QUOTES);    // html           
    return $post;
}

ie 6でjsonpが実行しない解決策を返す
phpでヘッダに戻る
header('cache-control:no-cache;');
header('Content-Encoding: plain');

またjsトリガボタンにreturn falseを追加する.ie 6でのajax割り込み問題の解決
ajaxがコミットしたデータはすべてutf 8形式で、phpは一般的にiconv(「utf-8」,「gbk//IGNORE」,$str)またはmb_を使用します.convert_Encoding($value,'gbk','utf-8')は変換され、火星文字が付いている場合、例えば:♡チリンチリン..チリンチリン..Merry Christmas!♡前者は特殊文字を消し、後者は特殊文字が疑問符になるのでajaxFormPostコミット方式では符号化変換は不要