1

I'm creating a web app that uses MONGOHQ to store data, and that uses Sinatra to run the app. If I go to: localhost:4578/names.json, I get the names of all the names that I use for my data. However, I'm having trouble accessing this data using the getJSON method of jquery.

The file /names.json looks like this:

["Yasiel Puig","Nick Franklin","Mike Zunino","Jurickson Profar","Manny Machado"]

I tried doing something like this:

var series = []
$.get('names.json', function(n) {
n.forEach(function(s) {
  series.push({
    name: s
  })
})
}, 'json')

But this does not really work. Do you have any other ideas for how I should access the json data? Could I save it to a var? I'm pretty sure the json data is not JSONP format, so maybe I should treat it as AJAX?

Justus Romijn
  • 15,699
  • 5
  • 51
  • 63
Tim Kaye
  • 21
  • 2
  • How do you know it "*does not really work?*" Have any errors occurred? Is the issue that `series` seems to remain empty? – Jonathan Lonowski Aug 01 '13 at 07:22
  • A common beginner-writes-web-service issue is that you could be loading the html/javascript form the file system, and the browser will then block access to the web service. Have you arranged that the web service is also serving your html and javascript? – Neil Slater Aug 01 '13 at 07:31

3 Answers3

0

Your code seems to work, I tried it in this Fiddle. Therefore the problem is probably on server side.

var data = ["Yasiel Puig", "Nick Franklin", "Mike Zunino", 
"Jurickson Profar", "Manny Machado"];

var series = [];
data.forEach( function( e ) {
        series.push( {
            name: e
        });
    }
);

series.forEach( function( e ) {
     console.log( e.name );   
});
Matthias Holdorf
  • 1,040
  • 1
  • 14
  • 19
0

Could I save it to a var?

Possibly. Though, it depends on which variable and where/when you need it.

$.get() is asynchronous. It only starts the request, sets the callback as a listener, and then exits.

var series = [];

$.get('names.json', function (n) {
  n.forEach(function(s) {
    series.push({
      name: s
    });
  });

  // inside the callback...
  console.log(series); // filled: [ { name: "Yasiel Puig" }, ... ]
});

// after the request...
console.log(series);   // still empty: []

So, you can use series, or more importantly n, within the callback. Outside, it won't be available yet.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • thanks jonathan. If I were to save it as a var, how would I do so? – Tim Kaye Aug 01 '13 at 20:58
  • @TimKaye You already are -- `series`. But, *where* and *when* you use that variable are the important parts. For a thorough explanation, have a look at http://stackoverflow.com/q/14220321/. – Jonathan Lonowski Aug 01 '13 at 21:00
0

there is a difference between calling $.get('names.json') and $.get('/names.json') I think you are not adding the starting slash(/) to the url

when you call $.get('names.json') it calls complete_current_url + '/names.json'

eg. if you are on /home page then the url that would be called is /home/names.json

and $.get('/names.json') will call current_domain + '/names.json'

from any page it will always call '/names.json'

Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51