Egret和Http請求 (Ajax、XMLHttpRequest、Post、Get)


一  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM