目前正在访问和附加这样的AJAX响应:
$(document).ready(function() {
var $products = $('#products');
$.ajax({
type: 'GET',
url: 'http://localhost:9000/api/products/',
success: function(products) {
$.each(products, function(i, product) {
$products.append('<li><p>' + product.name + '</p><p>' + product.id + '</p></li>');
});
}
});
});
但是在把手中访问它有点麻烦。 这是我得到的JSON响应:
[{"id":112502,"code":"501.161655","name":"S8000 FOLIAGE PACK","descriptions":{"short":"Men's Shoe","long":"Men's shoe. The retro running S8000 model is presented in an unexpected version, following the trend of camouflage prints. It has been reinterpreted with a foliage print which strongly characterises the upper and it nearly covers it all."},"price":{"sell":67.5,"currency":"€"},"images":{"small":"http://cdn01.diadora.com/upload/diadora/images/501.161655_30044_small.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.161655_30044_medium.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.161655_30044_big.jpg"},"options":[{"name":"Size","values":[{"name":"6.5"},{"name":"7"},{"name":"7.5"},{"name":"8.5"}]}]},{"id":90645,"code":"501.159877","name":"CONDOR REPTILE","descriptions":{"short":"Unisex Shoe","long":"Basket inspired, the sneaker Condor is developed in soft suede leather with leather inserts enriched by outline logo in contrast color and by reptile inserts on the backcounter. Available in two different color combination."},"price":{"sell":42.5,"original":85,"currency":"€"},"images":{"small":"http://cdn01.diadora.com/upload/diadora/images/15987700001_C5409_small_501_159877.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15987700001_C5409_medium_501_159877.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15987700001_C5409_big_501_159877.jpg"},"options":[{"name":"Size","values":[{"name":"3"},{"name":"3.5"},{"name":"4"},{"name":"4.5"},{"name":"5"},{"name":"5.5"},{"name":"6"},{"name":"7.5"},{"name":"8"},{"name":"8.5"},{"name":"9.5"},{"name":"10"},{"name":"10.5"},{"name":"11"},{"name":"11.5"},{"name":"12"}]},{"name":"Color","values":[{"name":"Beige Oyster","images":{"small":"http://cdn01.diadora.com/upload/diadora/images/15987700001_25043_small_501_159877.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15987700001_25043_medium_501_159877.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15987700001_25043_big_501_159877.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/15987700001_25043_color_501_159877.jpg"}},{"name":"Green Conifer / Green Flash","images":{"color":"http://cdn01.diadora.com/upload/diadora/images/15987700001_C5409_color_501_159877.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15987700001_C5409_big_501_159877.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15987700001_C5409_medium_501_159877.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15987700001_C5409_small_501_159877.jpg"}}]}]},{"id":129483,"code":"501.159657_20859_20900","name":"GAME L HIGH WAXED","descriptions":{"short":"Unisex Shoe","long":"Mid-cut shoes Inspired by the 70's tennis world, in full-grain leather with applied leather logo. Personalised and vulcanised box sole with waxing treatment."},"price":{"sell":50,"original":100,"currency":"€"},"images":{"small":"http://cdn01.diadora.com/upload/diadora/images/501.159657_55086_small_501_159657.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.159657_55086_medium_501_159657.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.159657_55086_big_501_159657.jpg"},"options":[{"name":"Size","values":[{"name":"3.5"},{"name":"4.5"},{"name":"5"},{"name":"5.5"},{"name":"6"},{"name":"6.5"},{"name":"7"},{"name":"7.5"},{"name":"8"},{"name":"8.5"},{"name":"9.5"},{"name":"10.5"},{"name":"11.5"}]},{"name":"Color","values":[{"name":"Advent Violet","images":{"color":"http://cdn01.diadora.com/upload/diadora/images/501.159657_55086_color_501_159657.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/501.159657_55086_small_501_159657.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.159657_55086_medium_501_159657.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.159657_55086_big_501_159657.jpg"}},{"name":"Blue Caspian Sea / White","images":{"color":"http://cdn01.diadora.com/upload/diadora/images/501.159657_C3635_color_501_159657.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/501.159657_C3635_small_501_159657.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.159657_C3635_medium_501_159657.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.159657_C3635_big_501_159657.jpg"}}]}]},{"id":138417,"code":"501.158623_2755_14512_19119_5964_20868_20872_12052_12072_12041_1","name":"TITAN II","descriptions":{"short":"Unisex Sports Shoe","long":"Inspired by the 80s running shoes seen on the feet of the most famous athletes. Characterised by the bidensity midsole, shock absorber on the rear and flex point, highlighted by contrasting E.V.A. bands. Touched-up materials and colours for a fresher, sportier look."},"price":{"sell":36,"original":90,"currency":"€"},"images":{"small":"http://cdn01.diadora.com/upload/diadora/images/501.158623_70429_small.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.158623_70429_medium.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.158623_70429_big.jpg"},"options":[{"name":"Size","values":[{"name":"3.5"},{"name":"4"},{"name":"4.5"},{"name":"6"},{"name":"6.5"},{"name":"7"},{"name":"8"},{"name":"9"},{"name":"9.5"},{"name":"10.5"},{"name":"11"},{"name":"11.5"},{"name":"12"},{"name":"13"}]},{"name":"Color","values":[{"name":"Coffee Bean","images":{"small":"http://cdn01.diadora.com/upload/diadora/images/501.158623_30042_02_big_small_501_158623.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.158623_30042_02_big_medium_501_158623.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.158623_30042_02_big_big_501_158623.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/501.158623_30042_02_big_color_501_158623.jpg"}},{"name":"Ash Grey / Blue Denim","images":{"color":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C4959_color_501_158623.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C4959_small_501_158623.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C4959_medium_501_158623.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C4959_big_501_158623.jpg"}},{"name":"Sky-Bl Malibu' / Red","images":{"small":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C5295_small_501_158623.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C5295_medium_501_158623.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C5295_big_501_158623.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/15862300001_C5295_color_501_158623.jpg"}},{"name":"Dark Gull Grey / Coffee Bean","images":{"small":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5576_02_big_small_501_158623.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5576_02_big_medium_501_158623.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5576_02_big_big_501_158623.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5576_02_big_color_501_158623.jpg"}},{"name":"Grey Alaska / Fer.red Italy","images":{"small":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5755_02_big_small_501_158623.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5755_02_big_medium_501_158623.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5755_02_big_big_501_158623.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/501.158623_C5755_02_big_color_501_158623.jpg"}}]}]},{"id":74497,"code":"501.159663","name":"CONDOR CAMO WAXED","descriptions":{"short":"Unisex Shoe","long":"Diadora basket inspiered men's shoe. An interpretation in line with the trends for a classic by Diadora. The whole effect is enhanced by a waxed treatment and studs in application."},"price":{"sell":45,"original":90,"currency":"€"},"images":{"big":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_big_501_159663.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_medium_501_159663.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_small_501_159663.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_color_501_159663.jpg"},"options":[{"name":"Size","values":[{"name":"3"},{"name":"3.5"},{"name":"4"},{"name":"4.5"},{"name":"5.5"},{"name":"6"},{"name":"6.5"},{"name":"7"},{"name":"8"},{"name":"10.5"},{"name":"11.5"},{"name":"12"}]},{"name":"Color","values":[{"name":"Burnt Olive Green","images":{"color":"http://cdn01.diadora.com/upload/diadora/images/15966300001_70431_color_501_159663.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15966300001_70431_small_501_159663.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15966300001_70431_medium_501_159663.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15966300001_70431_big_501_159663.jpg"}},{"name":"Gray / Beige Ivory","images":{"big":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_big_501_159663.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_medium_501_159663.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_color_501_159663.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15966300001_C5441_small_501_159663.jpg"}}]}]},{"id":77666,"code":"501.159159","name":"MI BASKET SHARK","descriptions":{"short":"Unisex Shoe","long":"Summer restyling of the Mi Basket, the iconic Diadora basket shoes, in a fresh and modern key. The shoes upper is completely in nylon with leather inserts. Available in three different colorway."},"price":{"sell":42.5,"original":85,"currency":"€"},"images":{"small":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5154_02_secondbig_159159000ok01_small_501_159159.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5154_02_secondbig_159159000ok01_medium_501_159159.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5154_02_secondbig_159159000ok01_big_501_159159.jpg"},"options":[{"name":"Size","values":[{"name":"4.5"},{"name":"5"},{"name":"5.5"},{"name":"6"},{"name":"6.5"},{"name":"7"},{"name":"7.5"},{"name":"8"},{"name":"8.5"},{"name":"9"},{"name":"9.5"},{"name":"10"},{"name":"10.5"},{"name":"11"},{"name":"11.5"},{"name":"12"}]},{"name":"Color","values":[{"name":"Sky-Blue Clear Water","images":{"big":"http://cdn01.diadora.com/upload/diadora/images/15915900001_65113_02_biokg_big_501_159159.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15915900001_65113_02_biokg_medium_501_159159.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15915900001_65113_02_biokg_small_501_159159.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/15915900001_65113_02_biokg_color_501_159159.jpg"}},{"name":"Navy / Atollo Sky-Blue","images":{"big":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5154_02_secondbig_159159000ok01_big_501_159159.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5154_02_secondbig_159159000ok01_medium_501_159159.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5154_02_secondbig_159159000ok01_small_501_159159.jpg","color":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5154_02_secondbig_159159000ok01_color_501_159159.jpg"}},{"name":"Flame Red / Vibrant Yellow","images":{"color":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5299_02_secondbig_1ok5915900001_color_501_159159.jpg","medium":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5299_02_secondbig_1ok5915900001_medium_501_159159.jpg","big":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5299_02_secondbig_1ok5915900001_big_501_159159.jpg","small":"http://cdn01.diadora.com/upload/diadora/images/15915900001_C5299_02_secondbig_1ok5915900001_small_501_159159.jpg"}}]}]}]
到目前为止我尝试过的事情:
<script id="new-temp" type="text/template">
{{#each product}}
<h2>{{product.name}}</h2>
{{/each}}
</script>
但鉴于响应中的数组没有名称,我不确定我是否正确访问它。 AJAX的新手,以及一般的模板,会不会有人帮我弄清楚如何访问它?
答案 0 :(得分:0)
尝试这样的事情:
将您的代码包装在{"products":products}
<script id="new-temp" type="text/template">
{{#each products}}
<h2>{{name}}</h2>
{{/each}}
</script>