我现在对框架的工作有一些难以理解的地方,比如从as3发送数据。目前我有这个关于拉维的代码:

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.

我所怀疑的是,我是否理解如何继续接收拉雷维尔的信息,并确定我的AS3请求是否正确。

最佳答案

您必须注意请求主体和url参数之间的区别。在您的路由中,您正在定义一个不同于请求主体的“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'];
    ...
}

关于php - 通过Post将Json对象发送到Laravel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34406943/

10-11 01:35