简体   繁体   English

jQuery从JSON源附加图像并循环

[英]jQuery append image from JSON source and loop

I'm working on fetching data from a JSON URL in jQuery with $ajax call and I'm using bootstrap in the HTML. 我正在通过$ ajax调用从jQuery中的JSON URL提取数据,并且正在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? 该图像似乎没有出来,但是显示了损坏的img类的东西,是否有任何提示或建议?

And right now I'm only getting one car out, at data[0]. 而现在,我只在数据[0]下开出一辆车。

Now I'm using a $.each , but what if I would receive all cars? 现在我正在使用$.each ,但是如果我能收到所有汽车怎么办?

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>");
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM