jQuery 將 jQuery.ajax包裝成簡單函式來作 Ajax Request,最簡單的情況下,.ajax() 可以不帶任何參數直接使用。

html :

<table>
 <tr>
 <td height="16" width="7%">input</td>
 <td width="60%"><input name="input" type="text" id="input"/></td> 
 </tr>
 
</table>
 <input type="button" id="sendout" value="submit"style="font-size: 10pt; width: 50px; height: 25px;">
 </br></br>
 <td height="16" ><label id="output" /></td>

 

JQuery :

("#sendout").click(function() {
 ("#input").val();
 alert("true : "+("#input").val()); 
 
 .ajax({
 type :"GET",
 url : "/testServlet/myServlet",
 data : { 
 datafromtestFile : ("#input").val(), 
 },
 dataType: "text",
 success : function(happy) {
 ("#output").html(happy); 
 }
 })
 });

 

參數說明 :

ajax當中的參數:

url(String):指定要進行呼叫的位址。

data(Map):傳送至Server的資料,必須為Key/Value格式,GET請求中將附加在URL後面。

type(String):請求方式,POST/GET。(預設為GET)

dataType(String):預期Server傳回的資料類型,如果沒指定,jQuery會根據HTTP MIME Type自動選擇以responseXML或responseText傳入你的success callback。可選的資料類型有:
           xml:傳回可用jQuery處理的XML。
           html:傳回HTML,包含script tags。
           script:傳回可執行的JavaScript。
           json:傳回JSON。
           jsonp:JSONP 格式。在URL加上?callback=?參數,並在Server端配合送回此jsonp callback。
           text:傳回純文字字串。

success:請求成功時執行函式。
                function (data, textStatus) {
                    // data 可以是 xmlDoc, jsonObj, html, text, 但還是要參考datatype                          
                }



error:請求失敗時執行函式。
           function (xhr, ajaxOptions, thrownError) {
                   //通常ajaxOptions或thrownError只有一個有值
           }
                  


complete:請求完成時執行的函式(不論結果是success或error)。
                 function (XMLHttpRequest, textStatus) {
                   // the options for this ajax request
                 }

 

文章標籤
全站熱搜
創作者介紹
創作者 luway 的頭像
luway

柯佳思吃吃吃

luway 發表在 痞客邦 留言(0) 人氣(65,916)