如何使用jquery在HTML页面中显示json数据?

问题描述:

我在asp.net中获得了json数据。我想使用jquery在html页面中显示json数据。

我的json数据如下



{NewsByIdResult:[{CompanyLegalName :TCS商业信息,IndstryName:建筑材料,行业:83,新闻编号:7,新闻ID:4,NewsTopic:病态单位}}}





但无法阅读酒店'查询'。请帮助我先生...



我尝试过:



< script>

$(document).ready( function(){

调试器;

$ .ajax({

url:'http:// localhost:25012 / NewsRestService.svc / WrappedNewsById / 4',

数据:{},

contentType:application / json; charset = utf-8,

dataType:json,

cache:false,

})。success(function(data){

alert(data);

$( #data')。append(JSON.stringify(data));

});

})

< / script>


另一种代码类型:



< script type =text / javascript>

调试器;

var service ='http:// localhost:25012 / NewsRestService.svc / NewsById /';

$(document).ready (function(){

jQuery.support.cors = true;

$ .ajax(

{

输入:GET,

url:service +'4',

data:{},

contentType:application / JSON; charset = utf-8,

dataType:json,

cache:false,

成功:函数(数据){

alert(data);

var trHTML ='';

$ .each(data.newsid,function(i,item){

''

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

'

});

$('#blog-post-content-desc')。append(trHTML);

},

错误:函数(msg){

alert(msg.responseText);

}

});

});

< / script>

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>

(文档)。 ready(function(){

debugger;
(document).ready(function () {
debugger;


.ajax({

url:'http://本地主机:25012 / NewsRestService.svc / WrappedNe wsById / 4',

数据:{},

contentType:application / json; charset = utf-8,

dataType:json,

cache:false,

})。success(function(data) {

alert(数据);
.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>



另一种代码:



< script type =text / javascript>

调试器;

var service = 'http:// localhost:25012 / NewsRestService.svc / NewsById /';
('#data').append(JSON.stringify(data));
});
})
</script>

Another Type of Code:

<script type="text/javascript">
debugger;
var service = 'http://localhost:25012/NewsRestService.svc/NewsById/';