通过Post将Json对象发送到Laravel

时间:2015-12-22 01:04:18

标签: php actionscript-3 http laravel

我目前正在理解框架是如何工作的,如从as3发送数据一样。目前我在Laravel上有这个代码:

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

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

在AccountController上:

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

在我的AS3上,我目前正在为这些方法执行此操作:

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.

我的疑问是了解如何继续接收laravel中的信息并确定我的as3请求是否正确。

1 个答案:

答案 0 :(得分:2)

您必须注意请求正文和网址参数之间的区别。在你的路线中,你定义了一个'nome'参数,它与请求体不同,nome将始终是一个字符串。 如果你想从那个nome参数中获取数据,你的AS3代码应该是这样的:

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

如果您想从AS3发送JSON,请保留该代码,但您必须修改Laravel代码中的一些内容

// no need to set nome as a url parameter
Route::post('Register' ,'AccountController@Register');

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