来自Controller的Laravel json响应并使用Ajax在View中呈现

时间:2015-07-15 22:01:50

标签: php jquery ajax json laravel

我正在使用Laravel 5.1并且遇到一个小问题。

我正在从我的控制器发送来自Instagram的大多数热门媒体的JSON响应,并希望在我的视图中使用ajax获取该响应并在页面上向用户显示该响应而不加载整个页面。

我正在尝试获取用户名并在我的视图中将其显示给用户。

现在没有任何事情发生,控制台没有显示任何内容,它是空的。

这是我的代码:

电脑板:

public function PopularPics(City $city){

    try{
        $jsonData =  $city->getMostPopularPics();
        return response()->json($jsonData);
    }catch(\Exception $ex){
        return $ex->getCode();
    }

}

查看

 <ul id="theData">

    </ul>


    <script>
       $(function(){
          $("#JRequest").click(function(e){
              e.preventDefault();

              $.ajax({
                  type: "GET",
                  url: "popular",
                  dataType:"json",
                    success:function(items){
                        $.each(items.data,function(i,item){
                            console.log(item.user.username);
                        });
                    }
              });
          });

       });


    </script>

INSTAGRAM JSON

{
"data": [{
    "type": "image",
    "users_in_photo": [],
    "filter": "Gotham",
    "tags": [],
    "comments": { ... },
    "caption": {
        "created_time": "1296656006",
        "text": "ãã¼ãâ¥ã¢ããªå§ãã¦ä½¿ã£ã¦ã¿ãã(^^)",
        "from": {
            "username": "cocomiin",
            "full_name": "",
            "type": "user",
            "id": "1127272"
        },
        "id": "26329105"
    },
    "likes": {
        "count": 35,
        "data": [{
            "username": "mikeyk",
            "full_name": "Kevin S",
            "id": "4",
            "profile_picture": "..."
        }, {...subset of likers...}]
    },
    "link": "http://instagr.am/p/BV5v_/",
    "user": {
        "username": "cocomiin",
        "full_name": "Cocomiin",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg",
        "id": "1127272"
    },
    "created_time": "1296655883",
    "images": {
        "low_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "id": "22518783",
    "location": null
},
{
    "type": "video",
    "videos": {
        "low_resolution": {
            "url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4",
            "width": 480,
            "height": 480
        },
        "standard_resolution": {
            "url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4",
            "width": 640,
            "height": 640
        },
    "users_in_photo": null,
    "filter": "Vesper",
    "tags": [],
    "comments": {
        "data": [{
            "created_time": "1279332030",
            "text": "Love the sign here",
            "from": {
                "username": "mikeyk",
                "full_name": "Mikey Krieger",
                "id": "4",
                "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg"
            },
            "id": "8"
        },
        {
            "created_time": "1279341004",
            "text": "Chilako taco",
            "from": {
                "username": "kevin",
                "full_name": "Kevin S",
                "id": "3",
                "profile_picture": "..."
            },
            "id": "3"
        }],
        "count": 2
    },
    "caption": null,
    "likes": {
        "count": 1,
        "data": [{
            "username": "mikeyk",
            "full_name": "Mikeyk",
            "id": "4",
            "profile_picture": "..."
        }]
    },
    "link": "http://instagr.am/p/D/",
    "user": {
        "username": "kevin",
        "full_name": "Kevin S",
        "profile_picture": "...",
        "id": "3"
    },
    "created_time": "1279340983",
    "images": {
        "low_resolution": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "id": "3",
    "location": null
},
...]
}

0 个答案:

没有答案