douyin2883 2014-10-26 14:30 采纳率: 0%
浏览 33
已采纳

使用jQuery将JSON发布到Laravel 4

I've been struggling with this for a while, I checked everything I could think of and I'm sure this should work.. but it doesn't. The idea is simple - you have a form input of type "text". When you type a number in the input and click "Click me!", it should POST the data in JSON format to a Route (handled via Closure), which would then check if the input is in JSON format and if yes, make a database request, then return the data.

Here is my view (table.blade.php)

<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
        <title>json test</title>
    </head>
    <body>
        <form action="#">
            <input type="text" name="articleID" id="articleid" placeholder="Enter article id" value=""/>
        </form>
        <a href="#" id="trigger">Click me!</a>
        <script>
        $(document).ready(function(){
            var a_id = $("#articleid").val();
            var article = { id: a_id };
            $("#trigger").click(function(){
                console.log(article);
                $.ajax({
                    type: "POST",
                    url: "/json",
                    data: article,
                    dataType: 'json',
                    success: function(data){
                        alert(data.title);
                    }
                });
            return false;
            });
        });
        </script>
    </body>
</html>

And my routes.php

Route::get('json',function(){
    return View::make('table');
});
Route::post('json',function(){
    if (Input::isJson())
    {
        $request = Input::all();
        $article = Article::find($request['id']);
        if (!is_null($article))
        {
            return Response::json($article);
        }
        else return Response::json(['error' => "Object not found"],404);
    }
    else return "not json";
});

I've got two problems with this:

  • console.log(article); prints Object { id=""} so JS doesn't seem to pick up the value of the input
  • No matter what, I always receive the response "not json", even if I replace data: article with something like data: {id: 123} in the ajax call

UPDATE Thanks to milz, the first issue is now fixed, I refactored the $(document).ready() function like so:

                $("#trigger").click(function(){
                    var a_id = $("#articleid").val();
                    var article = { id: a_id };
                    console.log(article);
                    $.ajax({
                        type: "POST",
                        url: "/json",
                        data: article,
                        dataType: 'json',
                        success: function(data){
                            alert(data.title);
                        }
                    });
                return false;
                });

Now the object is properly set, however the backend still returns only "not json"... I have no idea what I'm doing wrong here, I will appreciate any help! Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douhao3562 2014-10-26 14:51
    关注

    Input::isJson() actually checks for the Content-Type header of the request. The only thing dataType does is telling jQuery what response to expect.

    Try setting contentType

    $.ajax({
        type: "POST",
        url: "/json",
        data: article,
        dataType: 'json',
        contentType: 'application/json',
        success: function(data){
            alert(data.title);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。