Chicharito
Chicharito

Reputation: 1440

Jquery DropDownlist undefined item IE 8

$.get("CallBack.aspx", { nm: StateTx, nm2: StateTx2 }, function(data) {
                    $.each(data, function() {
                        $('[id$=DropDown1]').append("<option value=" + this['I3D'] + ">" + this['prmv'] + "</option>");
                    });
                });

my problem ie8 last item undefined. how do fix ?

Amain cm2 = new Amain();
DataTable dt = cm2.Getdt(str, str3);
StringBuilder sb = new StringBuilder();
sb.Append("[");
foreach (DataRow item in dt.Rows)
{
    sb.Append("{");
    sb.Append("\"prmv\":\"");
    sb.Append(item[0].ToString());
    sb.Append("\"" + "},");
}

sb.Append("]");

Context.Response.ContentType = "application/json";
Context.Response.ContentEncoding = Encoding.UTF8;
Context.Response.Write(sb.ToString());
Context.Response.End();

Upvotes: 0

Views: 852

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 994361

This can happen because of the way IE handles Javascript arrays. In IE,

var a = [1, 2, 3, 4,];

has five elements, the last of which is undefined. In Firefox, the last comma is ignored so the list has four elements.

Have a look at the exact data your Callback.aspx is returning.

Update: Your callback code causes this problem on the line:

                sb.Append("\"" + "},");

This will produce an array like:

[{"prmv":"1"},{"prmv":"2"},{"prmv":"3"},{"prmv":"4"},]
                                                    ^ extra comma

There is an extra comma before the ] of the returned array. One way to fix this might be to:

sb.Remove(sb.Length-1, 1); // remove extra trailing comma
sb.Append("]");

This fix will work for both IE and Firefox (and all other browsers).

Upvotes: 1

Related Questions