通过 Post 将 Json 对象发送到 Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34406943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Json Objects to Laravel via Post
提问by abr
I'm currently having some dificulty understanding how the framework works, as in sending data from as3. Currently I have this code on 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
On AccountController:
在 AccountController 上:
public function Register($nome){
return "How are you";
}
On my AS3, I'm currently doing this for these methods:
在我的 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.
The doubt im having is understanding how to proceed to receive information in laravel AND determine if my as3 request is correct.
我的疑问是了解如何在 laravel 中接收信息并确定我的 as3 请求是否正确。
回答by Sergio Guillen Mantilla
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:
您必须注意请求正文和 url 参数之间的区别。在您的路由中,您定义了一个与请求正文不同的“nome”参数,nome 将始终是一个字符串。如果你想从那个 nome 参数中获取数据,你的 AS3 代码应该是这样的:
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
如果你想从 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'];
...
}