problem with jquery and asp.net data exchange
i have a wcf service that i want to call and i know it's returning a JSON string.
Q1: how can i find out how the JSON looks like at runtime? for example, if the JSON is {'name':'steive', 'lastname':'jones'} how do i find this parameter names with javascript or jquery?
in the sample jquery like this:
$.ajax({
url: '/wcfService.svc/test',
dataType: 'json',
type: 'POST',
success: function(data) {
// to do
}
});
Q2: how do i know what names data.d contains in the success function??
as you can guess i'm trying to automate the server/client data exchange so if the JSON gets big the client should be able to use the parameters that could be in any names. in the above example i used name and lastname but the client should work with no problem if i change the name to names and lastname t开发者_运维技巧o lastnames at server side, like this: {'names':'steive', 'lastnames':'jones'} i should be able to use it without any modification to client. i'm using ASP.NET and C#.
Q3: is this even possible??
you sure can!
you can loop through data's properties youse the for ... in
loop. use .hasOwnProperty
to only get the properties that are part of your object and not the base object properties.
for (var prop in data ) {
if (data.hasOwnProperty(prop)) {
alert(prop + ' ' + data[prop]);
}
}
have a look at this fiddle to see it in action: http://jsfiddle.net/WNrgs/
精彩评论