0

I am using this jquery to populate a select element with options:

$("#ticket_customer").change(function() {
                $.ajax({
                    url: "?getContactList=2&customer=" + $(this).val(),
                    async: true,
                    success: function(data) {
                        var sel = $("#ticket_contact");
                        sel.empty();
                        for (var i=0; i<data.length; i++) {
                          sel.append('<option value="' + data[i].sequence + '">' + data[i].forename + '</option>');
                        }

                        //$("#ticket_contact").html(data);
                        GetCustomerDetails();
                    }
                });
            });

the request to the URL is returning data in the following format:

[{"sequence":"465","forename":"first1","surname":"second1","email":"[email protected]"},{"sequence":"465","forename":"first2","surname":"second2","email":"[email protected]"}]

but its not populating the element

2
  • I think you forgot dataType : "json" Commented Dec 9, 2015 at 22:22
  • Any errors in the Javascript console? Commented Dec 9, 2015 at 22:42

1 Answer 1

1

You have to add dataType to specify what format you are using:

$("#ticket_customer").change(function() {
                $.ajax({
                    url: "?getContactList=2&customer=" + $(this).val(),
                    async: true,
                    dataType : "json",
                    success: function(data) {
                        var sel = $("#ticket_contact");
                        sel.empty();
                        for (var i=0; i<data.length; i++) {
                          sel.append('<option value="' + data[i].sequence + '">' + data[i].forename + '</option>');
                        }

                        //$("#ticket_contact").html(data);
                        GetCustomerDetails();
                    }
                });
            });
Sign up to request clarification or add additional context in comments.

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.