---
title: "flex发送请求到后台"
author: "xiajava2011"
date: "2011-11-26T13:42:13Z"
tags: []
categories: ["Web前端"]
slug: "flex"
draft: false
lastmod: "2011-11-26T13:42:13Z"
id: 1278816
url: "https://xiajava2011.iteye.com/blog/1278816"
---

&lt;!--StartFragment --&gt;

交谈中请勿轻信汇款、中奖信息、陌生电话，勿使用外挂软件。

   BadBoy 2011-11-26 10:17:58  
 Flex使用httpService,需要后台语言的支持，即类似JSP，PHP，ASP.NET,ASP等语言的支持，另外还涉及到部署WEB服务器的相关问题。Flex使用httpService，可以使我们在前台向后台的程序发送数据，并由后台经过处理后返回结果给前台。

 下面我将介绍如何使用httpService组件。

 首先，新建一个Application程序，文件名如httpServcieDemo.mxml,代码如下：  
①用mxml方式  
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" borderColor="#1F80C3" themeColor="#138DDB" backgroundGradientAlphas="\[1.0, 1.0]" backgroundGradientColors="\[#00C09A, #0382AB]" layout="absolute" &gt;  
 &lt;mx:Script&gt;  
  &lt;![CDATA[  
   import mx.rpc.events.FaultEvent;  
   import mx.rpc.events.ResultEvent;  
   import mx.controls.Alert;  
     
   //发送出错时的处理函数  
   public function onFalutHandler(event:FaultEvent):void  
   {  
    Alert.show("发送或者接收时出现问题！");  
   }  
     
   //接收到结果后处理的函数  
   public function onGetResultHandler(event:ResultEvent):void  
   {  
    Alert.show("您接收到java传来的结果是:" + event.result.toString());  
   }  
     
   public function doSend():void  
   {  
     httpService1.url = "http://localhost:8080/test/index.jsp?txtMessage=";   
     httpService1.url += txtMessage.text;  
     httpService1.send();  
   }  
  ]]&gt;  
 &lt;/mx:Script&gt;  
 &lt;mx:HTTPService id="httpService1"      useProxy="false"    resultFormat="text"   fault="onFalutHandler(event)" result="onGetResultHandler(event)"/&gt;  
 &lt;mx:Label text="发送信息：" color="#1261E4" fontWeight="bold" fontSize="16" x="136" y="53"/&gt;  
 &lt;mx:TextInput x="225" y="57" width="368" id="txtMessage"/&gt;   
 &lt;mx:Button label="发送" x="336" y="107" id="buttonSend" width="62" color="#0945F1" fontSize="13" click="doSend();"/&gt;   
&lt;/mx:Application&gt;

②用ActionScript方式  
package com.zbkc.biz  
{  
import com.adobe.serialization.json.JSON;

import flash.net.URLRequest;  
import flash.net.URLVariables;

import mx.controls.Alert;  
import mx.rpc.events.FaultEvent;  
import mx.rpc.events.ResultEvent;  
import mx.rpc.http.HTTPService;

public final class Service  
{  
private var httpService:HTTPService;

public function Service()  
{  
httpService=new HTTPService();  
httpService.useProxy=false;  
httpService.method="POST";  
httpService.addEventListener(FaultEvent.FAULT,faultCallback);//发生错误的时候  
}

public function send(url:String,params:URLVariables,doLogicFunction:Function):void{  
// if(params){  
// Alert.show(url+"请求数据:\\n"+params.toString());  
// }else{  
// Alert.show(url+"请求数据:null");  
// }  
//增加监听事件  
httpService.addEventListener(ResultEvent.RESULT,function resultCallback(event:ResultEvent):void{  
var data:String = event.result.toString();  
//Alert.show(url+"响应数据:\\n"+data);  
var jd:Object = JSON.decode(data);  
var isSuccess:Boolean=jd.result;  
if(!isSuccess){  
if(jd.errorCode==-2000){  
flash.net.navigateToURL(new URLRequest("infolist.html"), "\_self");  
}else{  
Alert.show(jd.message);  
}  
}else{  
doLogicFunction(jd.data);  
}  
});

httpService.url=url;

httpService.clearResult();  
if (params){  
httpService.send(params);  
}else{  
httpService.send();  
}  
}

private function faultCallback(event:FaultEvent):void{  
Alert.show(event.fault.faultString);  
}  
}  
}