简体   繁体   中英

How to get the dataset values from Json result

Here I returned the dataset as Json result.When ia using the '$.each' function, i did not get the values.

This is my Javascript:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script>
  // TODO: Replace with the URL of your WebService app
  $(window).load(function () {
    var tableName="customtable.Testing";
    alert("Script Running");
    $.ajax({
      type: "POST",
      url: "/CMSPages/TestingService.asmx/GetAllCustomTableData",
      data: "{ 'customTableName': '" + tableName + "' }",
      dataType: "json",
      contentType: "application/json; charset=utf-8",
      success: function(data) {
        var results = data.d;
            $.each(results, function(i, result) {
             alert(result.ItemID);
            });
          },
          error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.responseText);     
          }
        });
  });
</script>

And this is my Json Response :

{"d":"{\"NewDataSet\": \r\n[\r\n{\"Table\": \r\n[\r\n{\"ItemID\":8,\"ItemGUID\":\"5ef65845-1910-4f71-be3e-c2c0a1e47105
\",\"ItemModifiedWhen\":\"\\/Date(1471582785000)\\/\",\"ItemOrder\":null,\"Email\":\"kewchang.lee@ucsf
.edu\",\"ItemCreatedWhen\":\"\\/Date(1471582785000)\\/\",\"ItemCreatedBy\":53,\"ItemModifiedBy\":53,
\"FirstName\":\"Kewchang\",\"LastName\":\"Lee\"}\r\n]\r\n}\r\n]\r\n}\r\n"}

Try this instead of each funcation

for (var key in result) {
    if (result.hasOwnProperty(key)) {
        alert(key + " -> " + result[key]);
    }
}

It seems that the JSON result is not an array. You should check to se if the result is actually an array before trying to iterate though it.

if (Array.isArray(data.d)) {
    $.each(data.d, function (i, result) {

    });  
}
else {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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