Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling json data using jQuery sent by using Newtonsoft.Json
    primarykey
    data
    text
    <p><strong>I have tried searching all possible matches of my problem and also have tried a couple of solutions but unfortunately none worked</strong><br> My backend code:</p> <pre><code>Person p; foreach(DataRow dr in dt.Rows) { p = new Person(); p.id = Convert.ToInt16(dr["Id"]); p.name = dr["Name"].ToString(); p.phone = Convert.ToInt64(dr["Phone"]); pList.Add(p); } string ans = JsonConvert.SerializeObject(pList, Formatting.Indented); </code></pre> <p>jQuery.ajax </p> <pre><code>function ShowData() { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/Data", data: "{}", dataType: "json", success: function (data) { alert(data.d); var list = { "Person": +data }; for (i = 0; i &lt; list.Person.length; i++) { alert('Id: ' + list.Person[i].Id + '/nName: ' + list.Person[i].Name + '/nPhone: ' + list.Person[i].Phone); console.log('Id: ' + list.Person[i].Id + '/nName: ' + list.Person[i].Name + '/nPhone: ' + list.Person[i].Phone); } console.log(list.Person.length); }, error: function (result) { alert("Error"); } }); } </code></pre> <p>Alert output</p> <pre><code>[ { "id": 1, "name": "Bhavik", "phone": 9601109585 }, { "id": 2, "name": "Xyz", "phone": 1234567890 }, { "id": 3, "name": "Abc", "phone": 9876543210 } ] </code></pre> <p><code>console.log(list.Person.length);</code> returns undefined and hence does not enters the for loop.. So to work out with it.. and why is it necessary to specify <code>contentType</code> while <code>dataType</code> already exist.. Also can I use <a href="http://api.jquery.com/jQuery.getJSON/" rel="nofollow">$.getJSON</a> instead of $.ajax.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload