James Ferguson
James Ferguson

Reputation: 71

Uncaught syntax error:

I have the following code: which returns uncaught syntax error :. If I remove the dataType i get no errors but i also get no data?

function myfunc() {
    var value = $("#firstselect").val();
    $.get("contactlist.php", 
          {SEARCH_PARAM: value },
          dataType: "json",
          function(data) {
              var options = '<option value="">Select one...</option>';
              for(var i = 0; i < data.length; i++) {
                  options += '<option value="' + data[i].id +'">'+data[i].name+'</option>';
              }
              $("#secondselect").html(options);
          }
    );
}

Upvotes: 0

Views: 331

Answers (2)

StilgarBF
StilgarBF

Reputation: 1060

you have the parameters in a wrong order. DataType has to be the last

try:

function myfunc() {
var value = $("#firstselect").val();
$.get("contactlist.php", 
      {SEARCH_PARAM: value }
      function(data) {
          var options = '<option value="">Select one...</option>';
          for(var i = 0; i < data.length; i++) {
              options += '<option value="' + data[i].id +'">'+data[i].name+'</option>';
          }
          $("#secondselect").html(options);
      },
      "json"
);
}

Upvotes: 0

Brian Driscoll
Brian Driscoll

Reputation: 19635

Your success function needs to come before data type, and the data type should just be a string literal, not a named value:

function myfunc() {
  var value = $("#firstselect").val();
   $.get("contactlist.php", 
   {SEARCH_PARAM: value },
    function(data) {
      var options = '<option value="">Select one...</option>';
      for(var i = 0; i < data.length; i++) {
        options += '<option value="' + data[i].id +'">'+data[i].name+'</option>';
      }
      $("#secondselect").html(options);
    },
   "json"
  );

}

Upvotes: 6

Related Questions