donglun2024
2013-12-23 10:37
浏览 293
已采纳

Laravel JSON从POST读取

Suppose I have a resourcefull route in Laravel 4.1, like next:

Route::group(['prefix' => 'api',], function()
{
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Headers: Content-Type');
    Route::resource('user','UserController');
});

And I have controller method store() like next:

    public function store()
    {
        $user = new User;
        $name = Input::get('name');
        $user->login_name = implode('.', explode(' ', strtolower($name)));
        $user->name = $name;
        $user->email = Input::get('email');
        $user->password = Hash::make('1');
//      $user->save();
        return Response::json([
                'error' => false,
                'user' => $user->toArray()
                ], 200);

    }

I am making post request either from backbone, or just from rest api test tool: the request is following:

{"phone":"123123123","description":"","name":"test","email":"test@test.com"}

The problem is that when I look at the response I see that request was empty :

{"error":false,"user":{"login_name":"","name":null,"email":null}}

What can be the problem?

图片转代码服务由CSDN问答提供 功能建议

假设我在Laravel 4.1中有一个资源丰富的路由,如下:

   Route :: group(['prefix'=>'api',],function()
 {
 header('Access-Control-Allow-Origin:*'); 
 header('Access  -Control-Allow-Headers:Content-Type'); 
 Route :: resource('user','UserController'); 
}); 
   
 
 

我有下一个控制器方法 store()

  public function store()
 {
 $ user = new User; \  n $ name = Input :: get('name'); 
 $ user-> login_name = implode('。',explode('',strtolower($ name))); 
 $ user-> name  = $ name; 
 $ user-> email =输入:: get('email'); 
 $ user-> password = Hash :: make('1'); 
 // $ user-&gt  ; save(); 
返回Response :: json([
'error'=> false,
'user'=> $ user-> toArray()
],200); 
 \  n} 
   
 
 

我正在从骨干网或者 只是来自rest api测试工具:请求如下:

  {“phone”:“123123123”,“description”:“”,“name”:“test”,  “email”:“test@test.com”} 
   
 
 

问题在于,当我查看响应时,我看到请求为空:

 <代码> { “错误”:假​​, “用户”:{ “LOGIN_NAME”: “”, “姓名”:NULL, “电子邮件”:空}} 
   
 
 

可能是什么问题?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyu7210 2014-01-02 03:37
    已采纳

    Set the following header in your request

    content-type:application/json
    

    or alternatively you can set it in the "App::before" filter

    App::before(function($request)
    {
        $request->headers->set('content-type','application/json');
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题