Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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 1 at 7: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 1 at 7:31
add comment (requires an account with 50 reputation)

3 Answers

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 );   
});
share|improve this answer
add comment (requires an account with 50 reputation)

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'

share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
thanks jonathan. If I were to save it as a var, how would I do so? – Tim Kaye Aug 1 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 stackoverflow.com/q/14220321. – Jonathan Lonowski Aug 1 at 21:00
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.