1

I know there are some people who asked same question and get answered. I already looked ino all of them, still I couldn't solve my issue. I am having a jquery snipet which send value to a handler and the handler process the value from JS and returns data as JSON data. JSON data has two sets of records( two rows from database) which need to be catch through getJSON and process that. JSON data will look like

[{"Name":"P1","Description":"pd1",Value":"S1Test1"},{"Name":"P1","Description":"pd1","Value":"L1Test1"}]

My JS is

$(document).ready(function () {
    $.getJSON('ProfileHandler.ashx', { 'ProfileName': 'P1' }, function (data) {
        alert(data.Name);
    });
});

and my handler code is

string ProfileName = context.Request["ProfileName"];
GetProfileDataService GetProfileDataService = new BokingEngine.MasterDataService.GetProfileDataService();
IEnumerable<ProfileData> ProfileDetails = GetProfileDataService.GetList(new ProfileSearchCriteria { Name = ProfileName });
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string serProfileDetails = javaScriptSerializer.Serialize(ProfileDetails);
context.Response.ContentType = "text/json";
context.Response.Write(serProfileDetails);

What is the approach error here?

0

2 Answers 2

7

data is an array of objects

data[0].name

should be enough to fetch the first name.

To iterate over the entire array, you could do something like:

$.each(data, function(k, v){
    alert(v.name);
});

Where v is the current object in the array. Note that v == this

Sign up to request clarification or add additional context in comments.

1 Comment

You gave me more than what I asked, Thanks mate.
1

Your JSON defines an array, which has objects as entries. So instead of

alert(data.Name);

you'd want

alert(data[0].Name);

(and of course, other indexes as well, in your example you have 0 and 1).

(The quoted JSON is also invalid [missing " before the first Value], but I'm guessing that's a typo in the question.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.