Ad

Sending Json Objects To Laravel Via Post

- 1 answer

I'm currently having some dificulty understanding how the framework works, as in sending data from as3. Currently I have this code on Laravel:

Route::get('HelloWorld',function(){return "Hello World";});
//Method returns a Hello World - works

Route::post('Register/{nome?}' ,'[email protected]');
//Method returns a string saying "How are you" - doesn't process

On AccountController:

public function Register($nome){
    return "How are you";
}

On my AS3, I'm currently doing this for these methods:

request.url = "http://myip/HelloWorld";
request.requestHeaders = [new URLRequestHeader("Content-Type", "application/json")];
request.method = URLRequestMethod.GET;

var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, receiveLoginConfirmation);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, notAllowed);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, notFound);
loader.load(request);
//Works


var variables: URLVariables = new URLVariables();
variables.nome = "Pedro";

request.url = "http://myip/Register";
request.requestHeaders = [new URLRequestHeader("Content-Type", "application/json")];
request.data = variables;
request.method = URLRequestMethod.POST;

var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, receiveRegisterConfirmation);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, notAllowed);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, notFound); 
loader.load(request);
//Trying to understand the error, it gives me httperror 500, if I comment request.data it gives me httperror 405.

The doubt im having is understanding how to proceed to receive information in laravel AND determine if my as3 request is correct.

Ad

Answer

You have to note the difference between the request body and the url parameters. In your route you are defining a 'nome' parameter, which is different than the request body, nome will always be a string. If you want to get data from that nome parameter your AS3 code should be like this:

request.url = "http://myip/Register/SomeNameLikePedro";

And if you want to send JSON from AS3 just keep that code but you have to modify some things in your Laravel code

// no need to set nome as a url parameter
Route::post('Register' ,'[email protected]');

public function Register($request) {
    $data = $request->all();
    // you can access nome variable like
    $nome = $data['nome'];
    $otherVariable = $data['otherVariable'];
    ...
}
Ad
source: stackoverflow.com
Ad