Js+Ajax,Get和Post在使用上的區別小結_javascript技巧

來源:互聯網
上載者:User

get和post方法最大的不同在於:

1.get方法傳值參數在url裡面,而post參數放send裡面

2.post方法必須加上

xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

下面執行個體可以看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");

POST和GET方法共用檔案

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>

POST方法檔案:

 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'];?>

GET方法檔案:

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在使用上的區別小結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.