简体   繁体   English

如何从angularjs接收到laravel的JSON数据

[英]how to receive JSON data from angularjs into laravel

Help me on this: 帮助我:

I am sending JSON data through angularjs to laravel : 我正在通过angularjs向laravel发送JSON数据:

My angularjs Json code like: 我的angularjs Json代码如下:

        $scope.addnew = {name:'',email:'',message:''};
        $scope.addnew.name=$scope.name;
        $scope.addnew.email=$scope.email;
        $scope.addnew.message=$scope.message;

          $http.post("url",$scope.addnew)
        .then(function mysuccess(response) {

           console.log(response.data);

          });

Note: I have predefined url in my file. 注意:我的文件中有预定义的url。

And I want to receive this JSON file in my laravel controller so that all data can be saved in my mysql table "contactemail" and fields of table are named as name for name, email for email and message for message of user. 我想在laravel控制器中接收此JSON文件,以便所有数据都可以保存在我的mysql表“ contactemail”中,并且表的字段分别命名为name的名称,email的电子邮件和user的消息。 As I am newbie in Laravel I am not able to do this correctly.Please Help me in this. 由于我是Laravel的新手,因此我无法正确执行此操作。请帮助我。

You can retrieve your input at Laravel using $request->input('your_json_key') . 您可以使用$request->input('your_json_key')在Laravel检索输入。

Example basic saving data: 基本保存数据示例:

$post = new Post();
$post->title = $request->input('title');
$post->save();

Those above code will create new post and save it to posts table (depends on your model setup). 上面的代码将创建新的帖子,并将其保存到posts表(取决于您的模型设置)。

For more information: https://laravel.com/docs/5.3/requests#accessing-the-request 有关更多信息: https : //laravel.com/docs/5.3/requests#accessing-the-request

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

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