How to perform a REST api all in sammy js

350 Views Asked by At

I am having issues with calling a rest api with sammyjs. I am creating a singlepage app, and so far it works in sammy js as my partial htmls are loaded. But my main aim is on call of a hashed url I want to call an api to get data and after the request is done render the page (on callback), but it am getting "jquery.js:9631 GET http://localhost:8080/home/stats 404 ()" when it tries to call the api.

<script>


    $(document).ready(function() {




        var app = $.sammy('#content', function () {

            this.use(Sammy.JSON)


            this.get('#/home', function (context) {

                //var loadOptions = { type: 'get', dataType: 'json', data: {query: variable}, };


                this.load('http://localhost:8080/home/stats')
                    .then(function(items) {

                        $('#content').load("partials/home.html");

                    });



            });



        app.run('#/home')



    });})



</script>
1

There are 1 best solutions below

0
Eshiett Oto-obong On

the problem is sammy js defaults to thinking the http request was a file. so i used jquery ajax instead and it worked.

this.get('#/home', function (context) {
                $.ajax({
                  url: "http://localhost:8080/home/stats",
                  type: 'GET',
                  dataType: 'json',
                  success: function(data) {
                      context.app.swap('');
                      context.load('partials/home.html')
                          .appendTo(context.$element())
                          .then(function (content) {
                              getFunctionality();

                          });
                  }
              });
            });