我有以下代码将POST数据发送到我的服务器:
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);
}
问题是,在我的服务器上,当我使用var_dump($_POST)
时,只返回一个空数组。我不知道为什么会这样。我在traces
和params
参数上完成了url
,其中包含相关数据。
我发送的参数是:
var customerDetails:Object = new Object();
customerDetails.first_name = firstNameField.text;
customerDetails.last_name = lastNameField.text;
customerDetails.dob = dobField.text;
customerDetails.email = emailField.text;
PHP list_headers()
检索到的标头返回:
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" }
我使用Air 16.0 for iOS和ActionScript 3
任何帮助都会很棒。我不知道这是否是跨域政策问题。
P.S。通过GET请求正常工作。
答案 0 :(得分:0)
使用右dataFormat
:
loader.dataFormat = URLLoaderDataFormat.VARIABLES
答案 1 :(得分:0)
As3代码有效。您需要从服务器端PHP代码改进..请尝试这个..
<?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;
?>