Question

I'm using Laravel 4 and am trying to update a (#articles) div with the new articles that are retrieved from an ajax request. When I inspect the page and view the Network section, I can see the POST requests being fired off and it's not showing any errors (eg, articles appear to be returned). However, unfortunately, the #articles div is not being updated with the new information. Yet, if I do a browser refresh, the new articles are displayed.

Routes.php

Route::any("/dashboard/latest_sa", [
    "as"    => "dashboard/latest_sa",
    "uses"  => "DashboardController@latest_sa"
]);

controllers/DashboardController.php

Class DashboardController extends \BaseController
{
...
   protected function latest_sa()
    {
      if( Request::ajax() )
      {
        // called via ajax
        $articles = Articles::orderBy('published_at', 'desc')->paginate(20);
        return json_decode($articles);
      } 
      else 
      {
        // fresh page load
        $articles = Articles::orderBy('published_at', 'desc')->paginate(20);
        return $articles;
      }
  }
...
}

app/views/dashboard/default.blade.php

...

@section("content")

// defined in /public/js/main.js
<script type="text/javascript">
    callServer();
</script>

<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">

    <h4>Latest Articles</h4>

    <div class="articles">
        <ul>
        @foreach ($articles as $article)
            <li>
                <img src="{{ $article->user_image }}" alt="{{ $article->article_title }}" />
                <a href="{{ $article->article_link }}">{{ $article->article_title }}</a>
                <div class="details">
                    <span class="author">{{ $article->author_name }}</span>
                    <span class="created">{{ Helpers::time_ago($article->published_at) }}</span>
                    <span class="symbol"><a href ="{{ $article->symbol_link }}">{{ $article->symbol_title }}</a></span>
                </div>
            </li>
        @endforeach
        </ul>
    </div>
    {{ $articles->links() }}
</div>

...

/public/js/main.js

function callServer()
{
    setInterval(function(){
        $.ajax({
            type: "POST",
            url: "dashboard/latest_sa",
            success:function(articles)
            {
                $(".articles").html(articles);
            }
        });
    },5000);
}

JS is hardly my strong suit, so I'm not sure what I'm doing wrong here.

And, for clarity sake, the reason why I'm trying to update all of the articles in the div is so that the Helpers::time_ago method also gets called, instead of just fetching the new articles. This way, it properly shows how long ago the article was published (eg, less than a minute ago, a minute ago, a hour ago, a day ago, etc) without refreshing the page. Essentially, I'm trying to kill two birds with one stone; update the div with the most recent articles, and update the remaining article's published_at attribute using my Helpers::time_ago method. If there is a more effective / efficient way of doing this, feel free to correct me. This seems rather crude, but since it's only for personal use and will never be used for commercial purposes, it suits my needs (not that that excuses bad code).

Nonetheless, from my fairly basic understanding, the JS should be doing the following steps:

1) Fire a POST request off to the /dashboard/latest_sa route

2) Execute the DashboardController@latest_sa action

3) Return a DB collection of all $articles ordered by the latest published date, and paginated

4) Pass the $articles collection back to the JS success attribute (as articles)

5) Fire the anonymous function, with the articles collection as an argument

6) Update the corresponding inner HTML with the results from the articles collection

The logic sounds right, so I'm pretty sure this is going to be a human error (98% of the time it is, after all. lol). Hopefully, someone here will be able to see the (probably glaring) problem in the logic and point me in the right direction.

In the meantime, I'm going to keep toying around with it.

I look forward to your thoughts, ideas, and suggestions. TIA.


EDIT:

Well, I found one of the problems; the articles div is a class, and in the JS I'm referring to it as an id. I fixed that, and now after the timeInterval, the article's div is "updated" but no results are being displayed (none, zippo, nadda).

Yet, if I directly access the /dashboard/latest_sa URI I get the valid JSON response that I'm expecting. So, albeit I am closer, I am still missing something.


EDIT 2:

Okay, in the controller, I made some changes which can be seen above, where I am now doing a json_decode on the $articles, before returning them to be passed into the view. With that in place, the articles are showing back up again after the timeInterval has elapsed, however, the new articles and the published_at for the existing articles are not being updated. After reviewing Inspect -> Network, it shows that the server is responding with a 500 Internal Server Error from the ajax POST request.

Hrm... Seems like I'm going in circles. Sounds like a good time to take a break and go for a walk. ;)


EDIT 3:

Well, I modified my Helpers class and added in the following method to check if the $article is a json object.

public static function isJson($string)
{
    json_decode($string);
    return (json_last_error() == JSON_ERROR_NONE);
}

app/views/dashboard/index.blade.php

@foreach ($articles as $article)
    <?php
        if( Helpers::isJson($article) )
        {
            $article = json_decode($article);
            // dd($article) // when uncommented it returns a valid PHP object 
        }
    ?>
    <!-- Iterate over the article object and output the data as shown above... -->
@endforeach

As you can see, (for the time being) inside of my view's foreach($articles as $article), I run Helpers::isJson($article) as a test and decode the object if it is json. This has enabled me to get passed the 500 Internal Server Error message, populate the articles div with the results on the initial load, and after the ajax POST request is fired off, I'm getting back a server response of 200 OK according to Inspect -> Network. However, after it updates the div, it doesn't show any articles.

Around, and around I go... I think it's time I take that break I keep murmuring about. ;)

Any thoughts, suggestions and / or ideas are greatly welcomed and appreciated.

Was it helpful?

Solution

At first, you should know that, when you return a collection from the controller/route, the response automatically turns in to a json response so, you don't need to use json_decode() and it won't work, instead, you may try something like this (from your controller for ajax):

$articles = Articles::orderBy('published_at', 'desc')->paginate(20);
return View::make('defaultAjax')->with('articles', $articles);

Since building the HTML in the client side using the json data received from server side would be tough for you so, you may return HTML from the server with the generated view instead of json, so you may try something like this in your success handler:

success:function(articles) {
    $(".articles").html(articles);
}

Now create a view for ajax response without extending the template like this:

//defaultAjax.blade.php used in the controller for ajax response

<ul>
    @foreach ($articles as $article)
        <li>
            <img src="{{ $article->user_image }}" alt="{{ $article->article_title }}" />
            <a href="{{ $article->article_link }}">{{ $article->article_title }}</a>
            <div class="details">
                <span class="author">{{ $article->author_name }}</span>
                <span class="created">{{ Helpers::time_ago($article->published_at) }}</span>
                <span class="symbol"><a href ="{{ $article->symbol_link }}">{{ $article->symbol_title }}</a></span>
            </div>
        </li>
    @endforeach
</ul>
{{ $articles->links() }}

Notice, there is no @extendds() or @section(), just plain partial view, so it'll be rendered without the template and you can insert the ul inside the .articles div. That's it.

OTHER TIPS

$("#articles").html(articles); ->> $(".articles").html(articles);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top