HTTP方法之 GET對比POSThtml
GET:從指定的資源請求數據, POST:向指定的資源提交要被處理的數據ajax
GET方法: 數組
請注意,查詢字符串(名稱/值對)是在 GET 請求的 URL 中發送的:瀏覽器
/test/demo_form.asp?name1=value1&name2=value2
有關 GET 請求的其餘一些註釋:緩存
POST方法:jsp
相對應的,post
1不會被緩存,2不會保留在瀏覽記錄中,3不能收藏書籤,4數據長度無限制。url
例子:spa
$("button").click(function(){ $.post("demo_test_post.asp", { name:"Donald Duck", city:"Duckburg" }, function(data,status){ alert("Data: " + data + "\nStatus: " + status); }); });
能夠發現POST是參數和請求一塊兒發送到參數是(URL)請求是數據name和citycode
這個ASP代碼以下
<% dim fname,city fname=Request.Form("name") city=Request.Form("city") Response.Write("Dear " & fname & ". ") Response.Write("Hope you live well in " & city & ".") %>
關於使用ajax向後臺傳值問題:jsp頁面代碼
用戶名:<input type="text" name="user" id="user" /> 郵箱:<input type="text" id="email" name="email" /> <div id="showuser"></div> <input type="button" value="獲取值" id="btnGet" onclick="getValue()" />
在js中,代碼以下
function getValue(){ $.ajax({ type:"post", url:"loadUser.action", data:{ user:$('#user').val(), email:$('#email').val() }, success: function(response, status, xhr){ console.log(response); $('#showuser').html(response[0].content); } }); };
注意使用 user:$('#user').val(), 得到到值其中'#user',起做用的是id="user"而不是name="user"(試試便可知道)。若是後臺Action的話能夠直接在後臺用相同的名稱,使用getset方法便可獲得值。
console.log(response),是讓返回的值在瀏覽器的console中輸出。
關於radio button和select集合如何在ajax js中獲取相應的值
<input id="userSex" name="userSex" type="radio" value="男" checked="checked" /> 男 <input id="userSex" name="userSex" type="radio" value="女" /> 女 <input id="userSex" name="userSex" type="radio" value="保密" /> 保密 <s:select list="listNums " listValue="numName " listKey="numId" name="numId" id="numId" headerKey="ol" headerValue="請選擇" value="bean.numId"></s:select>
下面js中是取值方法,都已經通過本身使用,(關於radio我以爲還挺複雜的,不知有人提供更簡單的不)
var sex=document.getElementsByName("userSex");//不能getElementById,ById又只會讀數組第一個值 var sexvalue; for(var i = 0; i < sex.length; i++) { if(sex[i].checked) sexvalue = sex[i].value; } //sexvalue就是所須要的值 var numId = document.getElementById('numId').value;//select選擇框更加簡單 這一句就OK了