假设我在 Flash 应用程序中有一个表单,其中包含两个字段,input1 和 input2。当用户完成填写此表单时,它会转到一个 php 页面。
目前,我正在使用 $_GET
方法发送数据。
像这样:
var request:URLRequest;
request = new URLRequest("http://site.com/page.php?data1="+input1.text+"&data2="+input2.text);
navigateToURL(request);
在 php 代码中:
$_GET["data1"];
$_GET["data2"];
但这样一来,信息就保留在 URL 中。如何通过 $_POST
发送?
最佳答案
在 AS 3 中 URLRequest您用来指定请求的类有一个 method可用于设置提交方法的 HTTP 选项的属性,您需要使用 URLRequestMethod 将其设置为 POST常量 POST以获得完美的形式,或者您可以使用“POST”字符串。
你可以找到一个comprehensive example在 snipplr
简而言之:
var url:String = "http://localhost/myPostReceiver.php";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
requestVars.foo = "bar";
// ... fill in your data
request.data = requestVars;
request.method = URLRequestMethod.POST;
// after this load your url with an UrlLoader or navigateToUrl
使用 Adobe Air 时,您需要使用 URLLoader类而不是 navigateToURL()因为以下花絮:
Parameters request:URLRequest — A URLRequest object that specifies the URL to navigate to.
For content running in Adobe AIR, when using the navigateToURL() function, the runtime treats a URLRequest that uses the POST method (one that has its method property set to URLRequestMethod.POST) as using the GET method.
基本上只要您想正确使用 POST set 方法,navigateToUrl 的文档也指出了这一点:
接下来在 php 中,您将在超全局变量中接收变量 $_POST数组,您可以在其中访问它:
<?php
$foo = $_POST['foo'];
/* $foo now contains 'bar'
assignment to another value is not necessary to use $_POST['foo']
in any function or statement
*/
关于php - navigateToURL 通过 POST 发送数据到 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11173762/