编程语言
首页 > 编程语言> > navigateToURL通过POST发送数据到php页面

navigateToURL通过POST发送数据到php页面

作者:互联网

想象一下,我在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”字符串.

你可以在snipplr找到comprehensive example

所以简而言之:

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,flash,actionscript-3
来源: https://codeday.me/bug/20190902/1787896.html