AJAX中POST與GET二種方式區別及如何?

來源:互聯網
上載者:User
我們知道在AJAX的open方法中第一個參數代表傳遞方式,根據很多資料所寫可以使用GET,POST,HEAD及其他任何伺服器所支援的傳遞方式進對變數進行傳遞。但是對於POST及GET實際上的區別在哪裡,在實際編碼過程中應該如何?呢?

(註:對於傳遞方式大家可參見RFC2068部分規定及說明)

在AJAX中send方法大家都知道,在一般情況下都是傳遞null值,但是在post方式下,send()方法中則傳遞的是參數。

對於POST中可以使用資料儲存資料,而對於GET方式時必須保證參數的唯一(好像是吧??)

在GET過程中參數是與URL一同被發送過的如。http://localhost/ajaxtest/proceajax.php?name=stven&pws=123

但是在POST過程的時候,所需要傳遞的資料資訊就可以把?號之後的直接拿過來即可。

post_data=name=stven&pws=123

再加上使用js的迴圈

if (ajax_request_type == "GET") {
if (uri.indexOf("?") == -1)
uri = uri + "?rs=" + func_name;
else
uri = uri + "&rs=" + func_name;
for (i = 0; i < args.length-1; i++)
uri = uri + "&rsargs[]=" + args[i];
uri = uri + "&rsrnd=" + new Date().getTime();
post_data = null;
} else {
post_data = "rs=" + func_name;
for (i = 0; i < args.length-1; i++)
post_data = post_data + "&rsargs[]=" + urlencode(args[i]);
}

function ajax_run(action_name,uri,request_type,postvar) {
var post_data;
ajaxok = ajax_init_object();

if(request_type == "POST"){
uri = uri + "?nocache=" + new Date().getTime();
}else{
uri = uri + "&nocache=" + new Date().getTime();
}

if(ajaxok){
ajaxok.open(request_type, uri, false);

if (request_type == "POST") {
ajax.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}

ajaxok.onreadystatechange = ajax_complete;
ajaxok.send(post_data);
ajaxok = null;
}
}

  • 聯繫我們

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