【问题标题】:Laravel - Remove JSON header before saving to DBLaravel - 在保存到数据库之前删除 JSON 标头
【发布时间】:2018-03-12 13:24:53
【问题描述】:

我对 Laravel 和 Json 还很陌生。

我希望将 JSON 数据直接保存到 MYSQL 表中。一切正常,但这是我得到的正在存储的输出:

HTTP/1.0 200 OK
Cache-Control: no-cache, private
Content-Type:  application/json
Date:          Sun, 01 Oct 2017 04:10:34 GMT

{"_token":"53jnwIYCnLu1jeHdSVcL75Mgw2OD6RmZAh7Ojdyy","name":"adfadfdafd"}

我希望这是保存在数据库中的输出,其他所有内容都被忽略:

{"_token":"53jnwIYCnLu1jeHdSVcL75Mgw2OD6RmZAh7Ojdyy","name":"adfadfdafd"}

这是我的控制器:

public function addleads(Request $request)
    {
        $lead = new Lead;
        $lead->lead_data = response()->json($request);
        $lead->save();   
    }

查看

           <!-- load jQuery -->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

            <!-- provide the csrf token -->
            <meta name="csrf-token" content="{{ csrf_token() }}" />
            <input class="name"></input>
            <button class="postbutton">Post via ajax!</button> 
            <script>
                $(document).ready(function(){
                    var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
                    $(".postbutton").click(function(){
                        $.ajax({
                            /* the route pointing to the post function */
                            url: "{{ route('addleads') }}",
                            type: 'POST',
                            /* send the csrf-token and the input to the controller */
                            data: {_token: CSRF_TOKEN, name:$(".name").val()},
                            dataType: 'JSON',
                            /* remind that 'data' is the response of the AjaxController */
                            success: function (data) { 
                                console.log(JSON.parse(data)); 
                            }
                        }); 
                    });
               });    
            </script>

【问题讨论】:

    标签: javascript php mysql json laravel


    【解决方案1】:

    根据你的代码,你可以使用这个

    public function addleads(Request $request)
    {
        $lead = new Lead;
        $lead->lead_data = response()->json($request)->getContent();
        $lead->save();   
    }
    

    如果你不是真的想得到响应json,而是请求json,你可以调用$request-&gt;json()或者json_encode($request-&gt;all())

    【讨论】:

    • 收到此错误:文件:“D:\xampp\htdocs\leadcapture\vendor\laravel\framework\src\Illuminate\Support\Str.php”行:334 消息:“Symfony 类的对象\Component\HttpFoundation\ParameterBag 无法转换为字符串"
    • 我认为,您可以使用$request-&gt;all() 而不是$request-&gt;json() 来获取所有参数。
    • @RajaAbbas 我已经根据你的旧代码更新了代码,这是一个 symfony 函数
    • 谢谢@HanlinWang,你太棒了。我使用了 json_encode($request->all()) 方法。
    【解决方案2】:

    您只需要从请求中提取所需的数据并保存,如下所示:

    public function addleads(Request $request) { 
        $lead = new Lead; 
        $lead->lead_data = json_encode($request->only('_token', 'name'));
        $lead->save(); 
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 2017-04-13
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多