Ajax Cross-Domain workarounds

Source: Internet
Author: User
Cross-Domain Post

(function ($) {Window.isie6=$.browser.msie && jquery.browser.version==6.0;jquery.extend ({ajaxformnums:0, Ajaxformpost:function (sURL, datas, callback,domains) {/*[surl= submit address, datas= to submit data to image, callback= callback, domain= 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> ');}); O F.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);

Call Mode:

$.ajaxformpost (' Http://localhost/api.php?act=say ', {Cont:cont}, function (data) {  alert (data);  });


Build a file in the root directory of the Web ajax_blank.php
The contents are:


and add JS on the called page:

document.domain= ' 51.com ';


PHP section

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


Cross-domain get

$.getjson ("http://localhost/api.php?callback=", {receiver:receiver},function (data) {  alert (data.info);  })


PHP Processing Section:

$ret [' info '] = iconv ("GBK", "UTF-8", "No user exists");  $result = Json_encode ($ret);  echo Request_check ($_get[' callback '). ' ('. $result. ') ';  $_get[' callback '] need to handle, prevent RSS attack  function Request_check ($post) {          if (!GET_MAGIC_QUOTES_GPC ())    //Judge Magic _QUOTES_GPC is open          {          $post = addslashes ($post);    The MAGIC_QUOTES_GPC does not open the filter for the submitted data          }          //$post = Str_replace ("_", "\_", $post);    Filter out the ' _ '          //$post = Str_replace ("%", "\%", $post);    Filter out the '% '          $post = NL2BR ($post);    Enter conversion          $post = Htmlspecialchars ($post, ent_quotes);    HTML tags convert             return $post;  }


IE6 JSONP return non-implementation of the workaround
In PHP return header plus

Header (' Cache-control:no-cache; ');  Header (' Content-encoding:plain ');


In addition, the JS trigger button plus return false; solve the Ajax interrupt problem under IE6
Ajax submitted data are UTF8 format, PHP is generally used iconv ("Utf-8", "Gbk//ignore", $str) or mb_convert_encoding ($value, ' gbk ', ' utf-8 ') to convert, And when encountered with the Martian text, such as: ♡ jingle. Jingle Bells. Merry christmas! ♡, the former will remove the special characters, the latter special characters will become question marks, so the use of Ajaxformpost submission method does not need to encode the conversion

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.