Sending Json Form from Flash AS3

meghana picture meghana · Mar 7, 2012 · Viewed 7.2k times · Source

i am having one form accepting json post in Asp.net which i need to call from Flash As3...

i am using below code to do that. I have seen some post in which they say its working fine. But i am encountering below Error

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

Here is my code.

        var messages:Array = new Array ();


        messages.push({"From":fromemailTxt.text,"To": ToemailTxt.text,"Body": BodyText.text,"Subject":SubjectText.text});

        var JsonObj:String = JSON.encode(messages);
        trace(JsonObj);

        var variables:URLVariables=new URLVariables(JsonObj);



        RequestURL= srvStringURL;

        var JSONLoader:URLLoader = new URLLoader();
        JSONLoader.dataFormat=URLLoaderDataFormat.TEXT;

        JSONLoader.addEventListener(IOErrorEvent.IO_ERROR, GetBookmarkURLError, false, 0, true);
        JSONLoader.addEventListener(Event.COMPLETE, parseBookmarkURLResult, false, 0, true);


        var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");

        var request:URLRequest = new URLRequest(RequestURL);
        request.requestHeaders.push(hdr);
        request.data=variables;
        request.method = URLRequestMethod.POST;

        try 
        {
            JSONLoader.load(request);
        }
        catch (error:ArgumentError) 
        { 
            trace("An ArgumentError has occurred."+error.errorID.toString()); 
        } 
        catch (error:SecurityError) 
        { 
            trace("A SecurityError has occurred."); 
        }
        catch (error:Error) 
        {
            trace("Unable to load requested document.");
        }

Anybody have any idea on this?? Thanks

Answer

Engineer picture Engineer · Mar 7, 2012

The error is, because you are passing incorrect string to URLVariables constructor. Do not use URLVariables. Instead pass data as string: request.data=JsonObj;