navigateToURL is not sending POST

Asked

Viewed 135 times

3

I’m trying to send parameters from actionscript to the php, I looked up the following page:

Send variables using POST

I did it right, but the moment you perform navigateToURL(urlRequest, '_self');, he always shows erro instead of the expected VALOR DE TESTE.

I’ve researched a lot of information but only presents 2007 results down. Does anyone know how to pass a parameter in a simple way of flash for php (using POST and not GET).

  • Really?? So here you go. NO flash in the first frame I put the code as in the link above and then the php file that is on the same link, this: http://answall.com/questions/10273/enviar-vari%C3%A1veis-using-post Thanks Guilherme lol.

  • Just for the record, Sopt has several questions about how to pass data from JS to PHP, in the end it is practically the same thing. I think it’s worth a search.

  • I want to create the variable in flash, send it to php, open the php file and show the value of the variable (not easy) in the php page.

  • and Sopt is what?

  • @dev Stack Overflow in English... the search is there in the top corner. Anyway, I believe the posted answer solves your problem.

1 answer

3


When rotating the script for Adobe Flash Professional IDE instead of the browser the program will convert POST for GET when to use navigateToURL(...);, for this reason you will never get the VALOR DE TESTE because he forces it to use GET

Using POST with navigateToURL

Note that to use POST is necessary (in addition to req.method = URLRequestMethod.POST;)

  • Change the content-type for req.contentType = "application/x-www-form-urlencoded";
  • And encode the variables like this variables.valor = encodeURIComponent("VALOR DE TESTE");

It should look something like:

var url:String = "http://localhost/post-test.php";
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();

variables.valor = encodeURIComponent("VALOR DE TESTE");

req.method = URLRequestMethod.POST;
req.contentType = "application/x-www-form-urlencoded";
req.data = variables;

navigateToURL(req, "_blank");

So that navigateToURL work with POST you will have to put the swf embedded in an HTML (assuming the file name is example.html):

<object type="application/x-shockwave-flash" data="meu-swf.swf"></object>

and run this HTML by a server like Apache, Ngnix or lighttpd accessing:

http://localhost/exemplo.html

A message will appear saying that the pop-up has been blocked, but then just release as requested by the browser to test.

Using POST with Urlloader.load

The navigationToURL uses pop-up and this can be a problem, especially because of the blockers, I recommend doing the process using only Flash and ActionScript, in other words we will not open a new window, one way to do this is to use URLLoader.load, code example (do not mix with other script):

var url:String = "http://localhost/post-test.php";
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();

variables.valor = encodeURIComponent("VALOR DE TESTE");

req.method = URLRequestMethod.POST;
req.contentType = "application/x-www-form-urlencoded";
req.data = variables;

var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, completo, false, 0, true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioError, false, 0, true);
urlLoader.load(req);

function completo(e:Event):void
{
    trace(urlLoader.data); // output: test
}

function ioError(e:IOErrorEvent):void
{
    trace(e);
}

Note: The use of IOErrorEvent.IO_ERROR is mandatory

To test this codes (both with load as to navigateToURL), you can use var_dump or print_r in your PHP, it would be a kind of simple and manual debugging, follow PHP:

<?php
echo 'GET:', PHP_EOL;
print_r($_GET);

echo 'POST:', PHP_EOL;
print_r($_POST);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.