Reputation: 9332
All,
I'm working on an Flash AS2 project that consumes and sends JSON.
I'm using http://json.org/json.as to receive and parse JSON - it works great.
Here's the trouble - one of the requirements is that the app POST JSON to a web service.
In prior projects, I've used LoadVars.sendAndLoad() with fine results.
The problem is that, with loadVars, you're sending name/value pairs. However, the particular service that I'm sending JSON to in this project requires that the JSON must be the body of the POST. That is, not a name/value pair.
Unless I'm mistaken, LoadVars won't let you do this.
So, I tried using XML.sendAndLoad:
dataAsString = JSON.stringify(json);
var send_xml:XML = new XML(dataAsString);
var response_xml:XML = new XML()
response_xml.onData = function(rawResponseData) {
trace (rawResponseData);
}
send_xml.sendAndLoad([url], response_xml, "POST");
... since XML.sendAndLoad() sends its data in the POST body. (Yes, I know this is ugly - 'new XML(jsonString)' can't result in a valid XML object. But... it works.)
However, the roadblock I run into here is that the data POSTed by Flash gets encoded - quotes are replaced with ""
". In some cases, this would be fine, but the service I'm sending the JSON to does not permit this.
So, I'm looking for any of the following solutions:
"
)For what it's worth, AS3 handles this beautifully. Here's an example of how easy this should be:
import com.adobe.serialization.json.*;
// requires the as3corelib from https://github.com/mikechambers/as3corelib
var request:URLRequest = new URLRequest("[the service URL]");
request.method = URLRequestMethod.POST;
request.contentType = 'application/json';
var jsonData:Object = {[application-specific data]};
request.data = JSON.encode(jsonData);
var loader:URLLoader = new URLLoader();
loader.load(request);
However, the rest of the application depends on an enormous amount of legacy AS2 code, so unfortunately, AS3 is not an option.
Many thanks in advance for any advice or suggestions!
Upvotes: 2
Views: 5254
Reputation: 82088
The only thing I can think of is MovieClip.loadVariables
, but that changes quotes to %20
. As far as I can tell, your only real options after that are either passing everything out through JS using ExternalInterface or use LocalConnections to send data to an AS3 swf. (Both are equally hideous).
Upvotes: 1
Reputation: 58014
Have you considered using an intermediate script as a proxy? I mean a url that receives the name=value pairs, then posts the value to the main url and return the results?
Upvotes: 1