3 Replies Latest reply on Jan 10, 2011 1:07 PM by JiaYiyu

    How to send JSON data in HTTPService to PHP web service question

    MacLeodHP

      I'm using Flex 4 and a mx:HTTPService to send a JSON request to a php web service. I'm not sure if I'm sending the request correctly. Could someone look at the code below to see what I'm doing wrong?

       

      thanks

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:mx="library://ns.adobe.com/flex/mx"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     width="100%" height="100%">
          <fx:Declarations>
              <mx:HTTPService id="service" url="https://my web service url/"
                              method="POST" resultFormat="text" result="onResult(event)" fault="onFault(event)">
              </mx:HTTPService>
          </fx:Declarations>
         
          <fx:Script>
              <![CDATA[
                  import com.adobe.serialization.json.JSON;
                 
                  import mx.collections.ArrayCollection;
                  import mx.rpc.events.FaultEvent;
                  import mx.rpc.events.ResultEvent;
                  import mx.utils.Base64Encoder;
                 
                  [Bindable]private var username:String = "retailer@corp.com";
                  [Bindable]private var password:String = "the password";
                  [Bindable]private var accountNumber:String = "49055";
                  [Bindable]private var anticipatedDeliveryDate:Number = 20101115;
                  [Bindable]private var purchaseOrder:String = "#123 for retailer";
                  [Bindable]private var detailRecords:Array = new Array();
                  [Bindable]private var key:String = "abc123";
                 
                  private function populateService():void {
                      populateProducts();   
                      setRequestHeader();
                      setRequestData();
                      service.send();   
                  }
                 
                  private function setRequestHeader():void {
                      var encoder:Base64Encoder = new Base64Encoder();
                      encoder.insertNewLines = false;
                      encoder.encode(key);               
                            
                      service.headers["Authorization"] = "VIP " + key;
                      service.headers["Content-Type"] = "application/json";
                  }
                 
                  private function setRequestData():void {
                      service.request.username = username;
                      service.request.password = password;
                      service.request.accountNumber = accountNumber;
                      service.request.anticipatedDeliveryDate = anticipatedDeliveryDate;
                      service.request.purchaseOrder = purchaseOrder;
                      service.request.detailRecords = detailRecords;
                     
                      // caching test
                      var rnd : Number = Math.round(Math.random()*1000);
                      service.request.cacheOff = rnd;

       

                  }
                 
                  private function populateProducts():void {
                     
                      var prod1:Object = new Object();
                      prod1.itemCodeOrUPC = "00241";
                      prod1.itemDescription = "Budweiser Keg 1/2 BBL";
                      prod1.quantityOrdered = 2;
                      prod1.orderUOM = "Keg";
                     
                      var prod2:Object = new Object();
                      prod2.itemCodeOrUPC = "00219";
                      prod2.itemDescription = "Budweiser 24/12 OZ CAN";
                      prod2.quantityOrdered = 4;
                      prod2.orderUOM = "Case";
                     
                      detailRecords.push(prod1);
                      detailRecords.push(prod2);
                  }
                 
                 
                 
                  private function onResult(event:ResultEvent):void
                  {
                      var json:Object = JSON.decode(event.result as String);
                  }
                 
                  private function onFault(event:FaultEvent):void
                  {
                      trace("Fault: " + event.fault.faultString);
                  }
                 
                 
              ]]>
          </fx:Script>

       

          <mx:Panel width="95%" height="100%" color="#000000">
              <mx:HBox width="100%">
                  <mx:Button label="Submit" click="{populateService()}"/>
              </mx:HBox>   
          </mx:Panel>
         
      </s:Application>