Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:16:47+00:00 2026-06-08T23:16:47+00:00

I’m unclear as to how I am supposed to load json from an external

  • 0

I’m unclear as to how I am supposed to load json from an external API with sammyjs.

This code works great:

this.get('#/contact', function(context) {
       this.load('somefile.json')
      .then(function(items) {
            $.each(items, function(i, item) {
              context.log(item);
            });
        });

    });

However the same json loaded over http fails:

this.get('#/contact', function(context) {
       this.load('http://samedomain/api/getdata')
      .then(function(items) {
            $.each(items, function(i, item) {
              context.log(item);
            });
        });

    });

When loading over http, sammy no longer sees the json as objects and appears to parse the data as text.

Just so everyone is clear this is not an issue with domain access.

 header('Access-Control-Allow-Origin: *');

Nor do I believe it’s an issue with the format of my json as it appears to work fine when loaded as a local file.

My rest api is also useing:

"Content-Type: application/json;

update:
I put this to use in wordpress and listing it here in the event that it helps someone else

(function($) {    
var app = $.sammy('#main', function() {
    this.use('Template');

     this.helpers({
            loadJSON: function(location, options, callback) {
                options = $.extend(options, {json: true});
                return new Sammy.RenderContext(this).load(location, options, callback);
            }
        });


    this.get('#/', function(context) {
        this.loadJSON('http://localhost/wp-somesite/wp-admin/admin-ajax.php?action=get_all_cases')
            .then(function(items) {
                $.each(items, function(i, item) {
                  context.log(item);
                });
            });
      });

    });

$(function() {
    app.run('#/');
});

})(jQuery);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:16:48+00:00Added an answer on June 8, 2026 at 11:16 pm

    The issue is with to do with how sammy.js determines the type of data it’s retrieving. When you load “mydata.json” the .json tells sammy.js that this is JSON data, however when loading “‘http://samedomain/api/getdata'” it assumes it’s just plain text data.

    I don’t know what the best approach would be but two possible solutions are either to change your routing or convert the loaded items to JSON using this.json(…) like so:

    this.get('#/contact', function(context) {
       this.load('http://samedomain/api/getdata')
      .then(function(items) {
            $.each(this.json(items), function(i, item) {
              context.log(item);
            });
        });
    
    });
    

    Make sure your app has loaded the JSON library with this.use(Sammy.JSON) and that the JSON plugin is loaded in your script definitions.

    Edit:
    Another option is you can write a custom function that knows to load JSON, here is an example plugin that you could use:

    Sammy.JSON.LoadJSON.js:

    (function($) {
        Sammy.JSON.LoadJSON = function(app) {
            app.helpers({
                loadJSON: function(location, options, callback) {
                    options = $.extend(options, {json: true});
                    return new Sammy.RenderContext(this).load(location, options, callback);
                }
            });
        }
    })(jQuery);
    

    app.js:

    this.use(Sammy.JSON.LoadJSON);
    
    var app = $.sammy('#mytag', function() {
        this.get('#/contact', function(context) {
            this.loadJSON('http://samedomain/api/getdata')
            .then(function(items) {
                $.each(items, function(i, item) {
                    context.log(item);
                });
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.