源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JS与Ajax Get和Post在使用上的区别实例详解

  • 时间:2022-12-20 05:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JS与Ajax Get和Post在使用上的区别实例详解
[b]get和post方法最大的不同在于:[/b][b] [/b] 1.get方法传值参数在url里面,而post参数放send里面 2.post方法必须加上 [b]xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");[/b] [b]下面实例可以看get方法[/b] xmlHttp.open("GET","for.php?text="+url,true); 在post里面表现为: xmlHttp.open("POST","for.php",true); xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); [b]POST和GET方法共用文件[/b] index.php
<script src="a.js" type="text/javascript"></script>
<a href="#" onClick="funphp100('o')">o</a>
<a href="#" onClick="funphp100('t')">t</a>
<a href="#" onClick="funphp100('x')">x</a>
<div id="php100"></div> 
[b]POST方法文件: [/b] a.js
var xmlHttp; 
function S_xmlhttprequest(){ 
if(window.ActiveXObject){ 
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){ 
xmlHttp=new XMLHttpRequest();
}
}
function funphp100(n){
var data = "text=" +n;  //多个参数的,往后加
S_xmlhttprequest();
xmlHttp.open("POST","for.php",true); 
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange=byphp;
xmlHttp.send(data);
}
function byphp(){
var byphp100=xmlHttp.responseText;
document.getElementById("php100").innerHTML=byphp100;
} 
for.php:
<?
echo $_POST['text'];
?> 
[b]GET方法文件:[/b] a.js:
var xmlHttp; 
function S_xmlhttprequest(){ 
if(window.ActiveXObject){ 
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){ 
xmlHttp=new XMLHttpRequest();
}
}
function funphp100(url){
S_xmlhttprequest();
xmlHttp.open("GET","for.php?text="+url,true); 
xmlHttp.onreadystatechange=byphp; 
xmlHttp.send(null);
}
function byphp(){
var byphp100=xmlHttp.responseText;
document.getElementById("php100").innerHTML=byphp100;
} 
for.php:
<?
echo $_GET['text'];
?>
以上所述是小编给大家介绍的JS与Ajax Get和Post在使用上的区别实例详解的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部