首页 > 代码库 > iframe式ajax调用示例

iframe式ajax调用示例

1.新建 a.html 

<!doctype html> 
<html> 
<head> 
<meta charset=‘utf-8‘> 
<title>iframe式ajax调用</title> 
</head> 
<body> 
<form action=‘b.php‘ method=‘post‘ name=‘‘ id=‘‘ target=‘formTarget‘> 
<input type=‘text‘ name=‘username‘ id=‘‘ placeholder=‘‘ value=‘‘ /> 
<input type=‘submit‘ name=‘‘ value=‘提交‘ /> 
<span id=‘msg‘></span> 
</form> 
<iframe src=‘‘ name=‘formTarget‘ id=‘formTarget‘ style=‘display:none‘></iframe> 
</body> 
</html> 

2.新建 b.php 

<?php 
echo " 
<script> 
parent.document.getElementById(‘msg‘).innerHTML = ‘iframe式ajax调用成功!‘; 
alert(‘您输入的是:{$_POST[‘username‘]}‘); 
window.setTimeout(function(){ 
parent.window.location.reload(); 
},3000); 
</script> 

3.访问: http://localhost/a.html 

效果如图 
 

<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1400011021&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Franzige%2Fp%2F3725879.html&dt=1400011021696&shv=r20140508&cbv=r20140417&saldr=sb&correlator=1400011021604&frm=20&ga_vid=1003740855.1399798249&ga_sid=1399990330&ga_hid=1068210263&ga_fc=1&u_tz=480&u_his=785&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=tahoma&dfs=12&adx=0&ady=1081&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=WXkstKANwX&p=http%3A//www.cnblogs.com&dtd=48" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1400011021&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Franzige%2Fp%2F3725879.html&dt=1400011021770&shv=r20140508&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1400011021604&frm=20&ga_vid=1003740855.1399798249&ga_sid=1399990330&ga_hid=1068210263&ga_fc=1&u_tz=480&u_his=785&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=tahoma&dfs=12&adx=304&ady=1331&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=g86yFDqRzP&p=http%3A//www.cnblogs.com&dtd=47" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>