首页 > 技术文章 > Egret和Http请求 (Ajax、XMLHttpRequest、Post、Get)

gamedaybyday 2016-11-23 23:45 原文

一  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

推荐阅读