3

I am try to bind the dropdowmlist using jquery. But is showing some error.

Code:

                     $.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      data: "{product: '" + product + "'}",
                      url: "Search.aspx/FetchCategory",
                      dataType: "json",
                      success: function(data) {
                      $.each(data.d, function() {
                              $("#ddlCategory").append($("<option></option>").val(this['ID']).html(this['Category']));
                          });
                      }
                  });

values in the data: [{"Category":"All","ID":"%"},"Category":"Action,"ID":"4"},"Category":"Race,"ID":"5"},"Category":"Sports,"ID":"6"}]

Error:

$("#ddlCategory").append($("").val(this['ID']).html(this['Category'])); Microsoft JScript runtime error: Object doesn't support this property or method

Geetha

1
  • JavaScript is case-sensitive; the field in your data is 'Category' while your attempting to access 'category'. Commented Apr 10, 2010 at 6:00

1 Answer 1

6

Geetha: It looks like data.d is returning an array which contains objects?

Try implementing success like this:

success: function(data) {
    $.each(data.d[0], function(key,value) {
     $("#ddlCategory").append($("<option></option>").val(key).html(value));
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

I also want to insert id in all the options.So how can i do that??? <option id="opt1" value="opt1">option1</option>
You can modify the element builder "<option></option>" to contain as much or as little as you want. For example, you could do '<option id="'+key+'"></option>' or just "<option />". Notice how when adding attributes manually, I've used double quotes for the attributes and single quotes for the string passed to jQuery. I believe double quotes are XHTML standard (? maybe?) and this simplified escaping the quotes.

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.