A站(www.a.com),有个form表单,ajax提交到 B站(www.b.com),还附带个验证码
A站上的表单代码如下
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script>1 z G ! Y Y; <script> function send(){ $.ajax({ cache:true, type:"POST", url:"http://www.b.com/plus/diy.php", data:$('#diyform').serialize(), xhrFields:{withCredentials: true}, //ajax跨域提交需要x h D 9 l K crossDomain:true, //ajax跨域提交需要 suc7 N ; ( G + -cess: function(data){ if(data=="success"){ alert('发送成功!'); }else{ alert('发送失败!'); } } }); }y 5 1 </script> <f8 L u 8 Y W * $orm action="" enctype="multipart/form-data" method="post" im $ i ;d="` [ , 1 Ydiyform"> <in0 T 8 X | 7 r Iput type="hidden" name=, T \ B ["action" value="post"m - : x /> <input type="hiddG 7 W 1 ~ gen" n! \ 9 x k _ 4 lame="diyidq e S U 5 } ? $ e" value="2" /> <input type="hidden" name="do" valueA z H ~ B F="2" /&gw # b v ; $ A z Lt; <input type="hB ] D u J Uidden" name="dede_fields" value="name,text" /> <input type="hidden" name="dede_fieldshash" value="c2be5bV J o kb# a $ q5ec45d9d01ca9f60dc469b3fb" /&[ X $ :gt; &lG } Kt;p> 姓名: <input typer O u f } H h X ;='text' name='name' id='name' /> </p> <p> 验证码: <input type="text" name="validate"><img src="http://www.b.com/include/vdimgck.php" align="absmiddle" onclick="this.src='http:8 b & k k 1 x//www.b.com/include/vdimgck.php?tag='+Math.random();"> </p> <p onclick="send()">提交</p> </form>
1、提交到 http://www.b.com/plus/diy.php
2、验证码 http://www.b.com/include/vdimgck.php
3、ajax 跨域必需js代码
xhrFields:{withCredentials: true}, crossDomain:t{ 3 a N A & d }rue,
B站上的 /plus/diy.php: L G s ^ - 文件上面加入
header("Access-Control-Allow-Credentials:true"); header("Access-Control-Allow-Origin:http://www.a.com");
(此图片来源于网络,如有侵权,请联系删除! )