web基礎知識(一)關於ajax傳值最基礎東西


HTTP方法之 GET對比POST

  GET:從指定的資源請求數據,  POST:向指定的資源提交要被處理的數據

  GET方法: 

  請注意,查詢字符串(名稱/值對)是在 GET 請求的 URL 中發送的:

/test/demo_form.asp?name1=value1&name2=value2

  有關 GET 請求的其他一些注釋:

    • GET 請求可被緩存
    • GET 請求保留在瀏覽器歷史記錄中
    • GET 請求可被收藏為書簽
    • GET 請求不應在處理敏感數據時使用
    • GET 請求有長度限制
    • GET 請求只應當用於取回數據

  POST方法:

    相對應的,

    1不會被緩存,2不會保留在瀏覽記錄中,3不能收藏書簽,4數據長度無限制。

 

  例子:

  

$("button").click(function(){
  $.post("demo_test_post.asp",
  {
    name:"Donald Duck",
    city:"Duckburg"
  },
  function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

  可以發現POST是參數和請求一起發送到參數是(URL)請求是數據name和city

  這個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" />&nbsp;&nbsp;<input id="userSex" name="userSex" type="radio" value="女" />&nbsp;&nbsp;<input id="userSex" name="userSex" type="radio" value="保密" />&nbsp;&nbsp;保密

<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了

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM