1

I don't think this is a repeat because I can't find a specific answer anywhere. I'm new to JavaScript and can't figure out how to pull JSON information in using Ajax and display it on my page. I know I'm doing something wrong from simply not understanding the languages that well.

My JSON looks like this:

{ "lots" : [
{
    "name" : "NE Corner HW4 & 41st",
    "info" : "$2/Hr<br>Monthly Parking Available",
    "center" : {
        "lat" : 57.659390,
        "lng" : -127.414754
    },
    "topLeft" : {
        "lat" : 57.659616,
        "lng" : -127.415102
    },
    "bottomRight" : {
        "lat" :57.659208,
        "lng" :-127.414371
    }
}, ...etc
]}

And here's the Ajax call (this may well be wrong):

var jsonFile = $.ajax({
  method: "GET",
  url: "filepath/filename.json",
  dataType: "JSON"
});

Then I need to use the info in several functions:

for (var i = 0; i < jsonFile.lots.length; i++) {
  marker = new google.maps.Marker({
  position: new google.maps.LatLng(jsonFile.lots[i].center.lat, jsonFile.lots[i].center.lng)
  marker.setMap(map);
}

What am I doing wrong (besides everything)? Is it my JSON array? Do I need to parse or stringify it? Maybe it's the Ajax call. The JavaScript? I feel like it's probably everything.

1
  • You should check jQuery API before questioning just because it has many opts than answers you'll have Commented Jul 27, 2015 at 5:23

4 Answers 4

1

You have to have a success inorder to process the response you get. Example

$.ajax({
    url: 'Your service url',
    type: 'GET' or 'POST',
    dataType: 'json',
    success: function(response){
         //Do what ever you want with the response variable
         //Do a console.log(response) to see what is coming as the response
    }
})

As per your example you can use the following.

jsonFile.done(function(response){
    //Do what ever your want with the response.
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, my Ajax call is now fixed. The major piece I was missing was the jsonFile.done(function(dataHere){}) part. Now I'm running into another issue caused by the google maps API which I will post elsewhere.
0

Perhaps the data does not exist when you trying to use it. Every ajax call is asynchronous. Try this:

$.get("file_path_here").done(
    function(data){
//do your staff here, data variable contains the json
    }
);

Comments

0

It's better to use callbacks instead because it'll be non-blocking that way. JQuery also has a getJSON function that decodes the returned string from an Ajax call into JSON and calls the callback with th JSON Object as the parameter.

$.getJSON('http://localhost/data.json', function(data){
    for (var i = 0; i < data.lots.length; i++)
        marker = new google.maps.Marker({
        position: new google.maps.LatLng(data.lots[i].center.lat, jsonFile.lots[i].center.lng)
        marker.setMap(map);
        });
}

See https://api.jquery.com/jQuery.getJSON/

Comments

0
$.ajax({
    url: 'Your service url',
    type: 'GET',
    dataType: 'json',
    url: "filepath/filename.json",
    success: function(data){
        console.log(data);
     }
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.