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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测