Get folder and files recursively in SharePoint using REST call

There was a simple requirement I came across for which a page should list all the files and folders in a SharePoint document library.

I came up with a solution using ODATA call. Following is the REST call I made to get the list of all folder and files.

/_api/web/Lists/GetByTitle(‘Documents’)/Items?$select=FileLeafRef,FileRef&$orderby=FileRef asc

Following is the code which quickly prints them out in a table. Just add this code to a script editor web-part.
Please make sure the URL is updated based on your site URL.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
    $.ajax({
        url: "<site url>/_api/web/Lists/GetByTitle('Documents')/Items?$select=FileLeafRef,FileRef&$orderby=FileRef asc",
        type: "GET",
        headers: {
            "Accept": "application/json;odata=verbose"
        },
        success: function (data, textStatus, xhr)
        {
            $.each(data.d.results, function (index, item)
            {
                // alert("The items in list are : " + item.FileRef);
                $("#table1 tbody").append("<tr><td>" + item.FileRef + "</td></tr>");
            })
        },
        error: function r(xhr, textStatus, errorThrown)
        {
            alert("error:" + JSON.stringify(xhr));
        }
    });
</script>

<table id="table1">
    <tbody></tbody>
</table>