Javascript 尝试使用JQUERY用JSON数据填充Select

Javascript 尝试使用JQUERY用JSON数据填充Select,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,从ajax调用中检索数据似乎一切都很顺利,但我无法用JSON内容填充select,它会在控制台中不断触发此错误: Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"0":"1","s_id":"1","1":"RTG","s_name":"RTG"},{"0":"2","s_id":"2","1":"IR","s_name":"IR"},{"0":"3","s_id":"3","1":"NCR","

从ajax调用中检索数据似乎一切都很顺利,但我无法用JSON内容填充select,它会在控制台中不断触发此错误:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"0":"1","s_id":"1","1":"RTG","s_name":"RTG"},{"0":"2","s_id":"2","1":"IR","s_name":"IR"},{"0":"3","s_id":"3","1":"NCR","s_name":"NCR"},{"0":"4","s_id":"4","1":"RIG","s_name":"RIG"},{"0":"5","s_id":"5","1":"VND","s_name":"VND"}]
JS就是这个

function populateSelect(et_id){
    $.ajax({
        url: "http://localhost/new_dec/modules/Utils/searchAvailableStatus.php",
        type: "get", //send it through get method
        data:{et_id:et_id},
        success: function(response) {
            var $select = $('#newStatus');                          
            $.each(response,function(key, value) 
            {
                $select.append('<option value=' + key + '>' + value + '</option>');
            });
        },
        error: function(xhr) {
            //Do Something to handle error
        }
    });
}

假设服务器端脚本没有设置正确的内容类型:application/json响应头,则需要将响应解析为json

然后,您可以使用$.each函数循环数据:

var json = $.parseJSON(response);

假设服务器端脚本没有设置正确的内容类型:application/json响应头,则需要将响应解析为json

然后,您可以使用$.each函数循环数据:

var json = $.parseJSON(response);

我想你需要这样的东西

function populateSelect(et_id){
    $.ajax({
        url: "http://localhost/new_dec/modules/Utils/searchAvailableStatus.php",
        type: "get", //send it through get method
        data:{et_id:et_id},
        success: function(response) {
            var json = $.parseJSON(response);
            var $select = $('#newStatus');                          
            $.each(json ,function(index, object) 
            {
                $select.append('<option value=' + object.s_id+ '>' + object.s_name+ '</option>');
            });
        },
        error: function(xhr) {
            //Do Something to handle error
        }
    });
}

我想你需要这样的东西

function populateSelect(et_id){
    $.ajax({
        url: "http://localhost/new_dec/modules/Utils/searchAvailableStatus.php",
        type: "get", //send it through get method
        data:{et_id:et_id},
        success: function(response) {
            var json = $.parseJSON(response);
            var $select = $('#newStatus');                          
            $.each(json ,function(index, object) 
            {
                $select.append('<option value=' + object.s_id+ '>' + object.s_name+ '</option>');
            });
        },
        error: function(xhr) {
            //Do Something to handle error
        }
    });
}

发布更多信息。您的JSON看起来像什么?更好的是,创建一个JSFIDLE。发布更多信息。您的JSON看起来像什么?更好的方法是创建一个JSFIDLE。好的,现在添加选项的过程很好,但加载方式如下:[object object][object object][object object][object object][object object][object object][object object]好的,现在添加选项的过程很好,但加载方式如下:[object object][object object object object][object object object object object object]