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

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   ext   

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,cityfname=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了

 

 

  

聯繫我們

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