3

I am developing my first AIR app (moving over from PHP/Javascript) and am at a stage where I want to send some data from the app back to a PHP script on my server. I have the following:

var url:String = "PHP URL HERE";
var request:URLRequest = new URLRequest(url);

var requestVars:URLVariables = new URLVariables();
requestVars.test = "1234";

request.data = requestVars;
request.method = URLRequestMethod.POST;
request.contentType = "application/xml;charset=utf-8";

var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, processSERPS, false, 0, true);
urlLoader.load(request);

I did have something else in the server side PHP script originally but for debugging I just changed it to dump the REQUEST array.

With the code above it will simply return nothing:

Array
(
)

But if I change the request method to Get:

request.method = URLRequestMethod.GET;

I receive:

Array
(
    [test] => 1234
)

Which tells me that the code is correct, it just isn't sending the post parameters for some reason.

I would just alter the code to use GET variables but unfortunately the data I need to send is too large hence the need for POST.

Any help appreciated!

KyleC
  • 201
  • 4
  • 8
  • isnt php code different depending on whether extracting POST or GET data? so i assume your php code only works with GET –  Feb 21 '13 at 21:52
  • Unfortunately the PHP part of this is the only part I am confident about. I am using REQUEST in the PHP which should show GET or POST. Even if I specifically set it to echo POST variables it doesn't show any. – KyleC Feb 21 '13 at 22:16
  • 3
    What happens if you remove the line with `request.contentType = "application/xml;charset=utf-8";`? This looks like the wrong content type for POST data to me. – nwellnhof Feb 21 '13 at 23:13
  • Yep, the answer below further clarified this for me. Setting content type to application/x-www-form-urlencoded fixed it. Thanks – KyleC Feb 22 '13 at 09:10

1 Answers1

3

For URLRequest.contentType:

If the value of the data property is a URLVariables object, the value of contentType must be application/x-www-form-urlencoded.

This is the default value, so just removing your assignment should do it.

Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
  • Thanks! I didn't originally set the content type but came across a post somewhere else where somebody set the type to XML and it made post values work correctly hence me modifying it. For some reason (I blame windows 8) if I leave it blank it doesnt work, but set it to 'application/x-www-form-urlencoded' and the post values come through. – KyleC Feb 22 '13 at 09:10