0

I am looping over a number of json files in a directory called "trips" and would like to output data from each file in the front end. However I keep getting errors "Syntax Error: Unexpected token :". Every page I've found related to this topic seems to point to the json files being improperly formatted but these ones definitely are not. For some reference this is the format of each file

{
"start_time": some time,
"coords": [some array of coords],
"end_time": some time
}

Here is my code where I try to access the data from each json file:

$(document).ready(function() {
        console.log("Start jsTrips");
        $.each(jsTrips, function(k, v) {
          var weblink = "http://localhost:8080/trips/" + v;
          console.log("GET " + weblink);
          weblink += "?callback=?"
          $.ajax({
            url: weblink,
            dataType:"jsonp",
            success:function(data){
                $("." + k).html(data.start_time);
            }
        });
        });
        console.log("Finished");
});

If you need more details than this let me know.

Thanks everybody!!

1
  • can you post the sample mock up json Commented Dec 25, 2018 at 14:38

1 Answer 1

1

jsonp and json are different formats. You want json

Change dataType and remove ?callback=? from url

$.each(jsTrips, function(k, v) {
  var weblink = "http://localhost:8080/trips/" + v;
  console.log("GET " + weblink);

  $.ajax({
    url: weblink,
    dataType: "json",
    success: function(data) {
      $("." + k).html(data.start_time);
    }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.