Actionscript 3 Air iOS POST data not sending with URLLoader

493 views Asked by At

I have the following code to send POST data to my server:

var headers:Array = [
    new URLRequestHeader("api_access_token", ACCESS_TOKEN),
    new URLRequestHeader("api_secret", SECRET),
    new URLRequestHeader("Content-type", "application/json")
];
var base_url = "http://example.com/";
var complete_func:Function;

function postRequest(url:String, params:Object, response_func:Function){
    complete_func = response_func;
    var request = new URLRequest(base_url+url);
    request.requestHeaders = headers;
    request.method = URLRequestMethod.POST;
    request.contentType = "application/json";
    request.data = JSON.stringify(params);

    //  Handlers
    var postLoader = new URLLoader();
    postLoader.addEventListener(Event.COMPLETE, parseResponse);
    postLoader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent){ trace(e); });
    postLoader.load(request);
}

function parseResponse(e:Event){
    var data = e.target.data;
    trace(data);
    var json_data = JSON.parse(data);
    complete_func(json_data);
}

Problem is, on my server, when I use var_dump($_POST) nothing but an empty array is returned. I don't know why this is the case. I have done traces on params and url parameters which contain relavent data.

The params I am sending are:

var customerDetails:Object = new Object();
    customerDetails.first_name = firstNameField.text;
    customerDetails.last_name = lastNameField.text;
    customerDetails.dob = dobField.text;
    customerDetails.email = emailField.text;

The headers retrieved by PHP's list_headers() returns:

array(6) { [0]=> string(24) "X-Powered-By: PHP/5.4.30" [1]=> string(38) "Expires: Thu, 19 Nov 1981 08:52:00 GMT" [2]=> string(77) "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" [3]=> string(16) "Pragma: no-cache" [4]=> string(44) "api_secret: example_secret" [5]=> string(50) "api_access_token: example_access" }

I am using Air 16.0 for iOS with ActionScript 3

Any help would be great. I don't know if this is a cross-domain policy issue.

P.S. Works fine with a GET request.

2

There are 2 answers

7
Andre Lehnert On

Use the right dataFormat:

loader.dataFormat = URLLoaderDataFormat.VARIABLES
0
Göksel TÜR On

As3 code is valid. You need improve from server side php code.. Please try this ..

<?php 

$body = @file_get_contents('php://input');
$jsn =  json_decode($body);
echo $jsn->first_name;
echo $jsn->last_name;
echo $jsn->dob;
echo $jsn->email;

?>