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

源码网商城

Js+Ajax,Get和Post在使用上的区别小结

  • 时间:2021-11-21 13:10 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Js+Ajax,Get和Post在使用上的区别小结
[b]get和post方法最大的不同在于:[/b] 1.get方法传值参数在url里面,而post参数放send里面 2.post方法必须加上 [b]xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");[/b] 下面实例可以看get方法
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;
 }
[b]for.php:[/b]
<?
echo $_POST['text'];
?>
[b]GET方法文件:[/b] [b]a.js:[/b]
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;
 }
[b]for.php:[/b]
<?
echo $_GET['text'];
?>
以上这篇Js+Ajax,Get和Post在使用上的区别小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部