通过Post发送Json对象到Laravel
问题描述:
我目前正在理解框架如何工作,如从as3发送数据。目前,我有Laravel验证码:通过Post发送Json对象到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
上的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.
遇到的疑问IM了解如何继续在laravel中接收信息,并确定我的as3请求是否正确。
答
您必须注意请求正文和url参数之间的区别。在你的路线中,你正在定义一个'nome'参数,它与请求体不同,nome将始终是一个字符串。 如果你想从诺姆参数数据的AS3代码应该是这样的:
request.url = "http://myip/Register/SomeNameLikePedro";
如果你想从AS3发送JSON只是不停的代码,但你必须修改你的Laravel代码中的一些事情
// 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'];
...
}
该死的,我完全忘了这个话题,我有这样的奋斗,那么XD谢谢你! – abr