Sanjay gautam

Sanjay gautam

  • 1.9k
  • 90
  • 3.2k

json with ajax call in c#

Sep 28 2019 2:59 AM
i have some code it produced incorrect url where am i wrong please suggest me
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. using System.Web.Services;  
  9. using Newtonsoft.Json;  
  10. using Newtonsoft.Json.Linq;  
  11.   
  12. namespace jsonserialize  
  13. {  
  14.     public class EmployeeAPIController : ApiController  
  15.     {  
  16.         [WebMethod]  
  17.         public static string SerializeDeserialize(string data)  
  18.         {  
  19.             DataTable dt = new DataTable();  
  20.             // Deserialize the data to DataTable.  
  21.             JArray arrays = (JArray)JsonConvert.DeserializeObject(data);  
  22.             if (arrays.Count > 0)  
  23.             {  
  24.                 foreach (JToken jToken in arrays[0])  
  25.                 {  
  26.                     dt.Columns.Add((jToken as JValue).Value.ToString());  
  27.                 }  
  28.                 int i = 0;  
  29.                 foreach (var array in arrays)  
  30.                 {  
  31.                     if (i > 0)  
  32.                     {  
  33.                         DataRow dr = dt.NewRow();  
  34.                         int j = 0;  
  35.                         foreach (JToken jToken in array)  
  36.                         {  
  37.                             dr[j] = (jToken as JValue).Value;  
  38.                             j++;  
  39.                         }  
  40.                         dt.Rows.Add(dr);  
  41.                     }  
  42.                     i++;  
  43.                 }  
  44.             }  
  45.             // Serialize the DataTable.  
  46.             return JsonConvert.SerializeObject(dt);  
  47.         }  
  48.     }  
  49. }  
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <title></title>  
  6.     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  
  7.     <script type="text/javascript">  
  8.         $(document).ready(function () {  
  9.             $('#btn').on('click', function () {  
  10.                 var arr =  
  11.                     [  
  12.                         ["EmployeeId", "Company", "EmployeeName", "Location"],  
  13.                         [211, "inyxa", "Sanjay", "Faridabad"],  
  14.                         [235, "Tata", "Shashank mishra", "Noida"],  
  15.                         [123, "Kabir infosystem", "Surya singh", "Gaziabad"],  
  16.                         [444, "Velocis", "Ajay kumar", "Merath"],  
  17.                         [595, "W3wila", "Virendra shakya", "Kanpur"],  
  18.                         [256, "Tech Mahindra", "Ayush trivedi", "Banaras"]  
  19.                     ];  
  20.                 var myjson = JSON.stringify(arr);  
  21.                 $.ajax({  
  22.                     type: 'POST',  
  23.                     url: "../api/EmployeeAPI",  
  24.                     data: "{'data':'" + myjson + "'}",  
  25.                     dataType: 'json',  
  26.                     contentType: "application/json; charset=utf-8",  
  27.                     success: function (response){  
  28.                         var employees = JSON.parse(response.d);  
  29.                     },  
  30.                     error: function (response) {  
  31.                         alert(response.d);  
  32.                     }  
  33.                 });  
  34.             });  
  35.         });  
  36.     </script>  
  37.     </head>  
  38. <body>  
  39.     <input id="btn" type="button" value="Serialize Deserialize" />  
  40.     <hr />  
  41.     <table id="tbl">  
  42.         <tr>  
  43.             <th>Id</th>  
  44.             <th>Name</th>  
  45.             <th>Company</th>  
  46.             <th>Location</th>  
  47.         </tr>  
  48.         <tr>  
  49.             <td></td>  
  50.             <td></td>  
  51.             <td></td>  
  52.             <td></td>  
  53.         </tr>  
  54.     </table>  
  55.     </body>  
  56. </html>  

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (3)