jquery - Call WebAPI from HTML5 page -


i trying bind json response webapi call in html5 page. not sure off. webapi returns json:

{    "id": 1,    "date": "2015-10-26t00:00:00",    "status": "initiated",    "action": {      "verificationactiontypeid": 0,      "verificationactiontype": null,      "verificationactiontakenid": 0,      "verificationactiontaken": null,      "verficationactioncreatedate": "0001-01-01t00:00:00",      "emailaddress": null,      "notes": null    },    "actions": [      {        "verificationactiontypeid": 0,        "verificationactiontype": "perform rinse flowcell",        "verificationactiontakenid": 0,        "verificationactiontaken": "skip",        "verficationactioncreatedate": "2015-10-26t10:04:05.093",        "emailaddress": null,        "notes": null      }    ]  }

this jquery code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>      <script type="text/javascript">      var uri = 'http://localhost/custom.webapi/api/action?verificationid=1';      var $result = $('#result');      var msg;      $(document).ready(function () {                       $.getjson(uri)              .done(function (data) {                  $.each(data, function (key, item) {                      alert("action: " + item);                      $('#actions tbody').append('<tr><td>' + item.id + '</td><td>' + item.status + '</td></tr>');                  });              })              .fail(function (jqxhr, textstatus, err) {                                      var error = $.parsejson(jqxhr.responsetext);                  msg = "failed action data error message " + error.message;                                      alert("message: " + msg);              });      });          </script>

my html:

<body>      <aside id="data">          <div id="result"> </div>          <table id="actions">              <thead>              <tr>                  <td>id</td>                  <td>status</td>              </tr>              </thead>              <tbody></tbody>          </table>      </aside>  </body>

this shows on html page:

html5 page

notice line have in javascript:
alert("action: " + item);

it shows alert messages this:

id

date

status

i want show results in html table. not sure doing wrong. can provide.

the item json returning not collection, single object. "each" function iterating on properties of object, rather through objects in collection. here's simplified version of question (mcve, should have submitted code question):

var data = json.parse('{"id":1,"date":"2015-10-26t00:00:00","status":"initiated","action":{"verificationactiontypeid":0,"verificationactiontype":null,"verificationactiontakenid":0,"verificationactiontaken":null,"verficationactioncreatedate":"0001-01-01t00:00:00","emailaddress":null,"notes":null},"actions":[{"verificationactiontypeid":0,"verificationactiontype":"perform rinse flowcell","verificationactiontakenid":0,"verificationactiontaken":"skip","verficationactioncreatedate":"2015-10-26t10:04:05.093","emailaddress":null,"notes":null}]}');    $.each(data, function(key, item) {    alert("action: " + item);    $('#actions tbody').append('<tr><td>' + item.id + '</td><td>' + item.status + '</td></tr>');  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <table id="actions">    <tbody>    </tbody>  </table>

you need decide object want loop on (of if loop necessary). have collection of actions, , html table named actions. perhaps want loop on actions returned in response, , add table?

var data = json.parse('{"id":1,"date":"2015-10-26t00:00:00","status":"initiated","action":{"verificationactiontypeid":0,"verificationactiontype":null,"verificationactiontakenid":0,"verificationactiontaken":null,"verficationactioncreatedate":"0001-01-01t00:00:00","emailaddress":null,"notes":null},"actions":[{"verificationactiontypeid":0,"verificationactiontype":"perform rinse flowcell","verificationactiontakenid":0,"verificationactiontaken":"skip","verficationactioncreatedate":"2015-10-26t10:04:05.093","emailaddress":null,"notes":null}]}');      $.each(data.actions, function (key, item) {       alert("action id: " + item.verificationactiontypeid);       $('#actions tbody').append('<tr><td>' + item.verificationactiontypeid + '</td><td>'+item.verficationactioncreatedate+'</td></tr>');  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <table id="actions">      <thead>          <th>action type id</th><th>verficationactioncreatedate</th>      </thead>      <tbody>      </tbody>  </table>


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -