0

I have an ajax call which returns the category list. I wish to extract each name from the list. my ajax looks like :

$(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "/Client/GetCategoryList",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function () { alert("Error"); }
            });
        });

if i am using "dataType: "json"," then in the alert i found the result like

[object Object],[object Object],[object Object],[object Object],[object Object]

and if i comment the "dataType: "json" then the result is like

[{"iID":56,"strName":"as","strDescription":"as","strImage":""},{"iID":24,"strName":"laptop","strDescription":"laptops","strImage":"uploads/2dell.jpg"}, {"iID":14,"strName":"mobile","strDescription":"handsets","strImage":"uploads/14sams.jpeg"},{"iID":46,"strName":"sds","strDescription":"dsd","strImage":"uploads/Category/46bg.jpg"}]

I just wanted to extract the category name or strName from the data as a hyperlink . Any help will be highly appreciable.Thanks in advance.

2
  • Your script is returning an array of 5 objects. You need to iterate over them and get the strName property of each. Commented Nov 20, 2013 at 9:59
  • Try this you can get your answer stackoverflow.com/questions/4317112/… Commented Nov 20, 2013 at 10:02

3 Answers 3

1
var names = $.map(data, function(v){
    return v.strName;
});
Sign up to request clarification or add additional context in comments.

1 Comment

@rns Np! I thought you could figure out the hyperlink part with this ;)
1

Since you're returning an array, you need to loop over it:

    success: function (data) {
        $.each(data, function(i, el) {
            console.log(el.strName);
        });
    },

Comments

-1

You can extract "strName" by using data.strName in success function of your ajax call;

Sorry i didn't see it is an array. you can try getJson method like:

$.getJSON(url, param ,function(data) { //url - return json page
   $.each(data, function() {
       alert(this.strName);
   });
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.