5 Replies Latest reply on Nov 15, 2007 1:31 AM by Greg Dove

    problems with loadVars POST

    DZ-015 Level 1
      I'm having a lot of trouble using loadVars to ping the server with sendAndLoad, delivering my json data string...

      What I want is a magical function I can use like this...

      magicalServerCallFunction(serverURL, functionToReturnResponseTo, MyJsonString);

      any ideas?

      The issues I'm having with loadvars are two fold...
      1 - it's crashing out my flash player... not sure why...
      2 - it's garbling my json strings...

      thanks for you help!
        • 1. Re: problems with loadVars POST
          Greg Dove Level 4
          I've used LoadVars to receive json data successfully, but never tried to send it.

          LoadVars sends it as urlencoded name/value pairs, so I can't see how you can send a json string without sending a variable name that the string is assigned to.

          By the look of what you're trying to do you'd be better off with remoting: e.g. amfphp. And then you won't have to worry about converting to/from json as well.

          If you want to persist with json...I might be able to help if you can answer a few questions:
          what json class are you using - is it the one from json.org?
          is the issue mainly with the sent string or the return string (if you're returning json as well)
          are you using php for the server side script (json_encode / json_decode for php 5.2+ etc)?

          • 2. Re: problems with loadVars POST
            DZ-015 Level 1
            > what json class are you using - is it the one from json.org?
            yea - the AS2 json class from json.org

            > is the issue mainly with the sent string or the return string?
            when I use myLoadVars.sendAndLoad I see nothing getting sent on the net tab of firebug... and then it errors out. And lately the flash player has also been hanging on attempted sends too... ug...

            > re you using php for the server side script (json_encode / json_decode for php 5.2+ etc)?
            python actually... and when I do occasionally make it through with data I get garbled json out the other end. The problem is that I need to create a property of my loadVars instance to store the json string in... For instance: myLoadVars.json = myJsonString

            Out the other side, that string has been messed with... and ideally I'd like to be able to just send the json as the body of the http post, rather than as a variable in the header.

            Any ideas?

            Also, you can feel free to poke around in the source - i've put it here... (it's still pretty simple - you won't drown) :: http://www.lofiart.com/files/chat.fla

            Thanks again!
            • 3. Re: problems with loadVars POST
              DZ-015 Level 1
              Just to sum it all up...

              I think problem 1 is that, like you said, loadVars causes the json string to be url encoded... that's causing the distortion in the string...

              problem 2 is that I'd like the json to be in the body of the post... rather than in a name value pair.
              • 4. Re: problems with loadVars POST
                Greg Dove Level 4
                For problem 1, I know how to fix it, and I'll write a LoadJSON class to do it as I think it will be useful. I'll put it up for download on my own website when its live. I hope that might be later tomorrow (Friday).

                For now, give this a shot:
                after you declare lvSender as a new LoadVars, put this line in
                lvSender.toString = function() {
                return json.stringify(this.json);
                }

                and where you have:
                var s:String = json.stringify(paramsData);

                //loadVars sends data
                lvSender.json = s;

                replace it with:
                lvSender.json =paramsData;

                I think that should work... let me know how it goes.
                • 5. Re: problems with loadVars POST
                  Greg Dove Level 4
                  BTW... problem 2 wasn't really a problem. The name/value pairs are sent in the body with the POST method. Its just that they urlencoded and in name/value pairs - and that's not what you want.