Note that there are some explanatory texts on larger screens.

plurals
  1. POSammyJS loading json from http
    primarykey
    data
    text
    <p>I'm unclear as to how I am supposed to load json from an external API with sammyjs.</p> <p>This code works great:</p> <pre><code>this.get('#/contact', function(context) { this.load('somefile.json') .then(function(items) { $.each(items, function(i, item) { context.log(item); }); }); }); </code></pre> <p>However the same json loaded over http fails:</p> <pre><code>this.get('#/contact', function(context) { this.load('http://samedomain/api/getdata') .then(function(items) { $.each(items, function(i, item) { context.log(item); }); }); }); </code></pre> <p>When loading over http, sammy no longer sees the json as objects and appears to parse the data as text.</p> <p>Just so everyone is clear this is not an issue with domain access. </p> <pre><code> header('Access-Control-Allow-Origin: *'); </code></pre> <p>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.</p> <p>My rest api is also useing:</p> <pre><code>"Content-Type: application/json; </code></pre> <p><strong>update:</strong> I put this to use in wordpress and listing it here in the event that it helps someone else</p> <pre><code>(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('#/'); }); </code></pre> <p>})(jQuery);</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload