Can't get data with Handlebars from JSON

342
January 29, 2017, at 11:58 PM

Here is fiddle with code: jsfiddle

{
  "status": {
    "success": true,
    "error": ""
  },
  "data": {
    "item": [
      {
        "name": "Test 1",
        "keywords": [
          "[asdad,dasdas]"
        ],
        "description": "xxzfrfa",
        "test": 12.3,
        "id": "584ab61344be2bd806ef276d"
      },
      {
        "name": "My test 2",
        "keywords": [
          "[zx,zz]"
        ],
        "description": "xz",
        "test": 53,
        "id": "584ab67b11b75c3a10fc1518"
      }
    ]
  }
}

...

<div id="product-container"></div>
<script id="product-template" type="text/x-handlebars-template">
    {{#each data.item}} 
        <h2>{{brand}}</h2>
    {{/each}}
</script>

What can I do to return data from JSON ?..it's not working now.. If anyone can help me. I've tried a lot of things.

Answer 1

The code you provided is working, there is nothing to render since there is no brand field.

Here is working example: https://jsfiddle.net/panamaprophet/nbn39wzu/

(since i didnt found a brand field in your example, i replaced it with name just for demonstration).

Answer 2

You can access the name of an item like below

{{#data.item}} 
    <h2>{{name}}</h2>
{{/data.item}}

Sample code below: (Removed ajax to get it working here)

Fiddle

Rent Charter Buses Company
READ ALSO
Add .next() everytime a function is called

Add .next() everytime a function is called

I basically want to know if its possible to add anothernext() every time the function is called, this is the function i have at the moment which sort of works but it always brings up the same post which isn't what I am looking for it to do

400
Special character for input suggestions in autocomplete

Special character for input suggestions in autocomplete

I have a textbox in which i need to show the suggestions from which a user can choose and select one of themhowever, suggestions only be shown when user enter @ character

414