Click here to Skip to main content
15,936,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i got json data in asp.net. i want to display the json data in html page using jquery.
my json data below

{"NewsByIdResult":[{"CompanyLegalName":"TCS BUSINESS INFORMATION","IndstryName":"Building Materials","Industry":83,"NewsCompId":7,"NewsID":4,"NewsTopic":"Sick Units"}]}


but couldn't read property 'query'.Please help me sir...

What I have tried:

<script>
$(document).ready(function () {
debugger;
$.ajax({
url: 'http://localhost:25012/NewsRestService.svc/WrappedNewsById/4',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
}).success(function (data) {
alert(data);
$('#data').append(JSON.stringify(data));
});
})
</script>

Another Type of Code:

<script type="text/javascript">
debugger;
var service = 'http://localhost:25012/NewsRestService.svc/NewsById/';
$(document).ready(function () {
jQuery.support.cors = true;
$.ajax(
{
type: "GET",
url: service + '4',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (data) {
alert(data);
var trHTML = '';
$.each(data.newsid, function (i, item) {
''

' + listvalues.newsid[0].NewsTopic + '

'
});
$('#blog-post-content-desc').append(trHTML);
},
error: function (msg) {
alert(msg.responseText);
}
});
});
</script>
Posted
Updated 21-Jun-16 2:26am
v4
Comments
F-ES Sitecore 21-Jun-16 7:52am    
Does your ajax call happen? Does the url get called? Does it return the data you want? Does the success event fire? Does your alert show? If so what is in the alert box? Does your append show anything on the screen? What do you want it to show on screen?
Karthik_Mahalingam 21-Jun-16 7:53am    
post the WrappedNewsById() code
Thiyagu Arockiasamy 21-Jun-16 8:22am    
I'm working in asp.net wcf rest service. below code is my service

public List<newss> NewsById(string NewsID)
{
string Status = TokenHelper.IsValidateToken();
using (DbConnection con = db.CreateConnection())
try
{
con.Open();List<newss> Lstnews = new List<newss>();
DbCommand cmd = db.GetStoredProcCommand("spNews_GetValues", NewsID);
cmd.CommandTimeout = int.MaxValue;
SqlDataReader objnewsRead = (SqlDataReader)db.ExecuteReader(cmd);
while (objnewsRead.Read())
{
Lstnews.Add(new Newss()
{
NewsID = objnewsRead.GetInt32(objnewsRead.GetOrdinal("NewsID")),
NewsCompId = objnewsRead.GetInt32(objnewsRead.GetOrdinal("NewsCompId")),
CompanyId = objnewsRead.GetInt32(objnewsRead.GetOrdinal("CompanyIds")),
CompanyLegalName = objnewsRead.GetString(objnewsRead.GetOrdinal("CompanyLegalName")),
});
}

return Lstnews;
}
finally
{
con.Close();
}
}

IService code:

[OperationContract]
[WebGet(UriTemplate = "WrappedNewsById/{NewsID}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
List<newss> NewsById(string NewsID);
Karthik_Mahalingam 21-Jun-16 8:24am    
ok fine,
what is the value you are getting in alert(data);
Thiyagu Arockiasamy 21-Jun-16 8:27am    
does not read it. not getting

1 solution

Your data is already in string notation, there is no need to stringify it anymore. That is why you get the error. (For this error, go to the fiddle I have provided, change the parse function call to stringify and then check the error in console.) What you need to do is, you need to parse it to an object. For that, you need to do the following,

JavaScript
// JSON
{
   "NewsByIdResult": [
        {
            "CompanyLegalName":"TCS BUSINESS INFORMATION",
            "IndstryName":"Building Materials",
            "Industry":83,
            "NewsCompId":7,
            "NewsID":4,
            "NewsTopic":"Sick Units"
        }
   ]
}

var obj = JSON.parse(data); // Parsed!

Now you can use the obj object to get the properties. Such as like this,
JavaScript
alert(obj.NewsByIdResult[0].CompanyLegalName);

// Should show TCS BUSINESS INFORMATION

And it just does, test it here, Edit fiddle - JSFiddle[^]

For more on JSON parsing, please read: JSON.parse() - JavaScript | MDN[^].

Stay tuned, my next article on JSON is also coming out... If you're interested. :-)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900