0

I'm working on fetching data from a JSON URL in jQuery with $ajax call and I'm using bootstrap in the HTML.

$.ajax({
  url: 'https://services.web.bilinfo.dk/api/vehicle/?user=demo&password=ocfB6XzF73&format=json',
  type: 'GET',
  data: {
    format: 'JSON'
  },
  error: function() {
    $('#info').html('<p>An error has occurred</p>');
  },
  success: function(data) {
    $.each(data, function(index, data) {
      $('.col-md-4')
        .append("picture" + '<img src= "' + data[0].Pictures + '">')
        .append("<h1> Model:" + data[0].Model + "</h1>")
        .append("<h1> make:" + data[0].Make + "</h1>")
        .append("<p> variant:" + data[0].Variant + "</p>")
        .append("<p> registrationDate:" + data[0].RegistrationDate + "</p>");
    })
  },
});

The image doesn't seem to come out, but displays a broken img like thing, any hints or suggestions?

And right now I'm only getting one car out, at data[0].

Now I'm using a $.each, but what if I would receive all cars?

1
  • 1
    Take care with your naming conventions. the data you pass as the first parameter to $.each is being overwritten by the data you pass as the second parameter to its function. Also, make sure the first data is actually an array. Commented May 6, 2018 at 13:53

1 Answer 1

1

I would generate a complete example, but the access control on that site does not allow it.

In your success handler try:

$.each(data.Vehicles, function(index, item) {
  $(".col-md-4")
    .append("picture" + '<img src= "' + item.Pictures[0] + '">')
    .append("<h1> Model:" + item.Model + "</h1>")
    .append("<h1> make:" + item.Make + "</h1>")
    .append("<p> variant:" + item.Variant + "</p>")
    .append("<p> registrationDate:" + item.RegistrationDate + "</p>");
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i got exactly the same, with data.Vehicles, that was what i was missing.
Check out @MikeMcCaughan comment. The $.each() function is now using item rather than data[0].

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.