標籤:form sub family asc 運行 status text log 應用
一 Http請求
二 AJax和XMLHttpRequest
三 一個Ajax例子
四 Egret中的egret.HttpRequest
五 Post和Get區別
一 Http請求
Http深入淺出 http://www.cnblogs.com/yin-jingyu/archive/2011/08/01/2123548.html
http請求返回碼 http://blog.chinaunix.net/uid-25311424-id-3052306.html
二 Ajax和XMLHttpRequest
根據w3school的AJAX Http請求描述:
AJAX 使用 Http 請求
在傳統的 JavaScript 編程中,假如您希望從伺服器上的檔案或資料庫中得到任何的資訊,或者向伺服器發送資訊的話,就必須利用一個 HTML 表單向伺服器 GET 或 POST 資料。而使用者則需要單擊“提交”按鈕來發送/擷取資訊,等待伺服器的響應,然後一張新的頁面會載入結果。
由於每當使用者提交輸入後伺服器都會返回一張新的頁面,傳統的 web 應用程式變得運行緩慢,且越來越不友好。
通過利用 AJAX,您的 JavaScript 會通過 JavaScript 的 XMLHttpRequest 對象,直接與伺服器來通訊。
通過使用 HTTP 要求,web 頁可向伺服器進行請求,並得到來自伺服器的響應,而不載入頁面。使用者可以停留在同一個頁面,他或她不會注意到指令碼在後台請求過頁面,或向伺服器發送過資料。
XMLHttpRequest 對象
通過使用 XMLHttpRequest 對象,web 開發人員可以做到在頁面已載入後從伺服器更新頁面!
在 2005 年 AJAX 被 Google 推廣開來(Google Suggest)。
Google 建議使用 XMLHttpRequest 對象來建立一種動態性極強的 web 介面:當您開始在 Google 的搜尋方塊中輸入查詢時,JavaScript 會向某個伺服器發出這些字詞,然後伺服器會返回一系列的搜尋建議。
XMLHttpRequest 對象得到下列瀏覽器的支援:Internet Explorer 5.0+、Safari 1.2、Mozilla 1.0 / Firefox、Opera 8+ 以及 Netscape 7。
三 一個Ajax例子
index.html
<script> var xmlhttp; function submit(score){ if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { console.log(xmlhttp.responseText); } } xmlhttp.open("POST","ajax.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("fname=" + score); } submit(999);</script>
ajax.php
<?php echo $_POST[‘fname‘];?>
四 Egret中的egret.HttpRequest
官方Http的教程:http://edn.egret.com/cn/index.php/article/index/id/589
Get方式
var request: egret.HttpRequest;request = new egret.HttpRequest();request.responseType = egret.HttpResponseType.TEXT;request.addEventListener(egret.Event.COMPLETE,this.onPostComplete,this);request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onPostIOError,this);
this.request.open("http://www.xxxx.com?data=123" , egret.HttpMethod.GET);
this.request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
this.request.send();
private onPostComplete(e:egret.Event):void{
var request = <egret.HttpRequest>event.currentTarget;
egret.log("get data : ",request.response);
}
private onPostIOError(e:egret.IOErrorEvent):void{
}
Post方式
var request: egret.HttpRequest;request = new egret.HttpRequest();request.responseType = egret.HttpResponseType.TEXT;request.addEventListener(egret.Event.COMPLETE,this.onPostComplete,this);request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onPostIOError,this);this.request.open("http://www.xxxx.com" , egret.HttpMethod.POST);this.request.setRequestHeader("Content-type","application/x-www-form-urlencoded");this.request.send( "p1=postP1&p2=postP2"); private onPostComplete(e:egret.Event):void{ var request = <egret.HttpRequest>event.currentTarget; egret.log("post data : ",request.response); } private onPostIOError(e:egret.IOErrorEvent):void{ }
五 Post和Get的區別
百度經驗 get和post提交方式的區別 http://jingyan.baidu.com/article/d3b74d64abbd6b1f76e60947.html
Egret和Http請求 (Ajax、XMLHttpRequest、Post、Get)