TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sanjay gautam
NA
90
14.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
using
System;
using
System.Collections.Generic;
using
System.Data;
using
System.Linq;
using
System.Net;
using
System.Net.Http;
using
System.Web.Http;
using
System.Web.Services;
using
Newtonsoft.Json;
using
Newtonsoft.Json.Linq;
namespace
jsonserialize
{
public
class
EmployeeAPIController : ApiController
{
[WebMethod]
public
static
string
SerializeDeserialize(
string
data)
{
DataTable dt =
new
DataTable();
// Deserialize the data to DataTable.
JArray arrays = (JArray)JsonConvert.DeserializeObject(data);
if
(arrays.Count > 0)
{
foreach
(JToken jToken
in
arrays[0])
{
dt.Columns.Add((jToken
as
JValue).Value.ToString());
}
int
i = 0;
foreach
(var array
in
arrays)
{
if
(i > 0)
{
DataRow dr = dt.NewRow();
int
j = 0;
foreach
(JToken jToken
in
array)
{
dr[j] = (jToken
as
JValue).Value;
j++;
}
dt.Rows.Add(dr);
}
i++;
}
}
// Serialize the DataTable.
return
JsonConvert.SerializeObject(dt);
}
}
}
<!DOCTYPE html
>
<
html
>
<
head
>
<
meta
charset
=
"utf-8"
/>
<
title
>
</
title
>
<
script
type
=
"text/javascript"
src
=
"https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"
>
</
script
>
<
script
type
=
"text/javascript"
>
$(document).ready(function () {
$('#btn').on('click', function () {
var
arr
=
[
["EmployeeId", "Company", "EmployeeName", "Location"],
[211, "inyxa", "Sanjay", "Faridabad"],
[235, "Tata", "Shashank mishra", "Noida"],
[123, "Kabir infosystem", "Surya singh", "Gaziabad"],
[444, "Velocis", "Ajay kumar", "Merath"],
[595, "W3wila", "Virendra shakya", "Kanpur"],
[256, "Tech Mahindra", "Ayush trivedi", "Banaras"]
];
var
myjson
=
JSON
.stringify(arr);
$.ajax({
type: 'POST',
url: "../api/EmployeeAPI",
data: "{'data':'" + myjson + "'}",
dataType: 'json',
contentType: "application/json;
charset
=
utf
-8",
success: function (response){
var
employees
=
JSON
.parse(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
});
</
script
>
</
head
>
<
body
>
<
input
id
=
"btn"
type
=
"button"
value
=
"Serialize Deserialize"
/>
<
hr
/>
<
table
id
=
"tbl"
>
<
tr
>
<
th
>
Id
</
th
>
<
th
>
Name
</
th
>
<
th
>
Company
</
th
>
<
th
>
Location
</
th
>
</
tr
>
<
tr
>
<
td
>
</
td
>
<
td
>
</
td
>
<
td
>
</
td
>
<
td
>
</
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
Reply
Answers (
3
)
System.Data.DataRowView does not contain a property ?
data is not convert to string