简体   繁体   English

如何在JSON对象中使用POST方法在Laravel中获取数据?

[英]How to get data in Laravel using POST method in JSON Object?

here is my route.php 这是我的route.php

Route::post('users' , array('uses'  => 'Userscontroller@index'));

This is my Userscontroller.php 这是我的Userscontroller.php

public function index()
    {
        $email_id = Input::get('email_id');
        $name = Input::get('name');

        return $email_id. ' ' .$name;
    }

When i run this in postman I get error as 当我在邮差中运行时,我得到错误

'>' unexpected in JSON 在JSON中出现 '>' 意外

.

Where m I missing? 我错过哪里?

Thank You. 谢谢。

if you send json like this: 如果你像这样发送json:

{"user":{"name":"abc","code":134}}

in laravel controller: 在laravel控制器:

$data =  $request->json()->all(); //read json in request
return response()->json($data);  //send json respond

Maybe you're looking for something like this: 也许你正在寻找这样的东西:

public function index()
{
   //if you want to get json data in your controller do this:
    Request::json()->all();

    //Return json data to your view script like this:
    return response()->json(['email_id'=>$email_id, 'name'=>$name]);
}

Try This: 试试这个:

 public function index(Request $request)
    {
       $dataArray = $request->get('data');
       $data = json_decode($dataArray, true);
       return 'Name: '.$data['name'].', Email: '.$data['email_id'];
    }

Send your data into data tag. 将数据发送到data标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM