flex发送请求到后台

<!--StartFragment -->
交谈中请勿轻信汇款、中奖信息、陌生电话,勿使用外挂软件。

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

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

 首先,新建一个Application程序,文件名如httpServcieDemo.mxml,代码如下:
①用mxml方式
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" borderColor="#1F80C3" themeColor="#138DDB" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#00C09A, #0382AB]" layout="absolute" >
 <mx:Script>
  <![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();
   }
  ]]>
 </mx:Script>
 <mx:HTTPService id="httpService1"      useProxy="false"    resultFormat="text"   fault="onFalutHandler(event)" result="onGetResultHandler(event)"/>
 <mx:Label text="发送信息:" color="#1261E4" fontWeight="bold" fontSize="16" x="136" y="53"/>
 <mx:TextInput x="225" y="57" width="368" id="txtMessage"/> 
 <mx:Button label="发送" x="336" y="107" id="buttonSend" width="62" color="#0945F1" fontSize="13" click="doSend();"/> 
</mx:Application>



②用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);
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的uniapp页面示例,演示如何在后台持续获取用户的位置信息并发送后台。需要注意的是,这个示例只是一个演示,实际使用中需要考虑到用户隐私等问题。 ```html <template> <div class="container"> <div class="location"> <p>经度: {{longitude}}</p> <p>纬度: {{latitude}}</p> <p>地址: {{address}}</p> </div> </div> </template> <script> export default { data() { return { longitude: '', latitude: '', address: '' } }, mounted() { // 获取初始位置信息 this.getLocation() }, onHide() { // 切换到后台时继续获取位置信息 uni.onHide(() => { this.getLocation() }) }, methods: { getLocation() { uni.getLocation({ type: 'gcj02', success: res => { this.longitude = res.longitude this.latitude = res.latitude // 发送位置信息到后台进行处理 this.sendLocation() }, fail: err => { console.log(err) } }) }, sendLocation() { // 发送位置信息到后台进行处理 // 使用uni.request发送请求 uni.request({ url: 'https://your-backend-url.com/location', method: 'POST', data: { longitude: this.longitude, latitude: this.latitude }, success: res => { this.address = res.data.address }, fail: err => { console.log(err) } }) } } } </script> <style> .container { display: flex; justify-content: center; align-items: center; height: 100vh; } .location { text-align: center; } </style> ``` 在这个示例中,我们在页面的mounted生命周期中获取了用户的位置信息,并将其发送后台进行处理。同时,我们使用了uni.onHide钩子函数,在切换到后台时继续获取位置信息并发送后台。在sendLocation方法中,我们使用了uni.request发送了一个POST请求,将用户的位置信息发送到了后台进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值