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。
           jsonpJSONP 格式。在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
                 }

 

arrow
arrow
    全站熱搜

    luway 發表在 痞客邦 留言(2) 人氣()