Ajax 進行Post傳值和Get傳值

來源:互聯網
上載者:User

以下AJAX post 傳值

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<script language="javascript">
function saveUserInfo()
{
//擷取接受返回資訊層
var msg = document.getElementById("msg");

//擷取表單對象和使用者資訊值
var f = document.user_info;
var userName = f.user_name.value;
var userAge   = f.user_age.value;
var userSex   = f.user_sex.value;

//接收表單的URL地址
var url = "/ajax_output.php";

//需要POST的值,把每個變數都通過&來聯結
var postStr   = "user_name="+ userName +"&user_age="+ userAge +"&user_sex="+ userSex;

//執行個體化Ajax
//var ajax = InitAjax();

          var ajax = false;
         //開始初始化XMLHttpRequest對象
         if(window.XMLHttpRequest) { //Mozilla 瀏覽器
                 ajax = new XMLHttpRequest();
                 if (ajax.overrideMimeType) {//設定MiME類別
                         ajax.overrideMimeType("text/xml");
                 }
         }
         else if (window.ActiveXObject) { // IE瀏覽器
                 try {
                         ajax = new ActiveXObject("Msxml2.XMLHTTP");
                 } catch (e) {
                         try {
                                 ajax = new ActiveXObject("Microsoft.XMLHTTP");
                         } catch (e) {}
                 }
         }
         if (!ajax) { // 異常,建立對象執行個體失敗
                 window.alert("不能建立XMLHttpRequest對象執行個體.");
                 return false;
         }
                
                
                

//通過Post方式開啟串連
ajax.open("POST", url, true);

//定義傳輸的檔案HTTP頭資訊
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

//發送POST資料
ajax.send(postStr);

//擷取執行狀態
ajax.onreadystatechange = function() {
   //如果執行狀態成功,那麼就把返回資訊寫到指定的層裡
   if (ajax.readyState == 4 && ajax.status == 200) {
    msg.innerHTML = ajax.responseText;
   }
}
}
</script>
<body >
<div id="msg"></div>
<form name="user_info" method="post" action="">
姓名:<input type="text" name="user_name" /><br />
年齡:<input type="text" name="user_age" /><br />
性別:<input type="text" name="user_sex" /><br />

<input type="button" value="提交表單" onClick="saveUserInfo()">
</form>

</body>

以下是AJAX get傳值

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<script language="javascript">
function saveUserInfo()
{
//擷取接受返回資訊層
var msg = document.getElementById("msg");

//擷取表單對象和使用者資訊值
var f = document.user_info;
var userName = f.user_name.value;
var userAge   = f.user_age.value;
var userSex   = f.user_sex.value;

//接收表單的URL地址
var url = "/ajax_output.php? user_name="+ userName +"&user_age="+ userAge +"&user_sex="+ userSex";

//執行個體化Ajax
//var ajax = InitAjax();

          var ajax = false;
         //開始初始化XMLHttpRequest對象
         if(window.XMLHttpRequest) { //Mozilla 瀏覽器
                 ajax = new XMLHttpRequest();
                 if (ajax.overrideMimeType) {//設定MiME類別
                         ajax.overrideMimeType("text/xml");
                 }
         }
         else if (window.ActiveXObject) { // IE瀏覽器
                 try {
                         ajax = new ActiveXObject("Msxml2.XMLHTTP");
                 } catch (e) {
                         try {
                                 ajax = new ActiveXObject("Microsoft.XMLHTTP");
                         } catch (e) {}
                 }
         }
         if (!ajax) { // 異常,建立對象執行個體失敗
                 window.alert("不能建立XMLHttpRequest對象執行個體.");
                 return false;
         }
                
                
                

//通過Post方式開啟串連
ajax.open("GET", url, true);

//發送GET資料,已經在URL中賦了值所以send那裡只要加個空參.
ajax.send(null);

//擷取執行狀態
ajax.onreadystatechange = function() {
   //如果執行狀態成功,那麼就把返回資訊寫到指定的層裡
   if (ajax.readyState == 4 && ajax.status == 200) {
    msg.innerHTML = ajax.responseText;
   }
}
}
</script>
<body >
<div id="msg"></div>
<form name="user_info" method="post" action="">
姓名:<input type="text" name="user_name" /><br />
年齡:<input type="text" name="user_age" /><br />
性別:<input type="text" name="user_sex" /><br />

<input type="button" value="提交表單" onClick="saveUserInfo()">
</form>

</body>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.