Ajax响应解析错误

时间:2015-06-01 20:37:56

标签: jquery ruby-on-rails ajax response

好的,我想知道如何解决这个错误:

Uncaught TypeError: Cannot use 'in' operator to search for '968' in "[["Acura", "acura"], ["Alfa Romeo", "alfaromeo"], ["Aston Martin", "astonmartin"]]"

错误的控制台信息:

isArraylike @   jquery.js?body=1:584 
jQuery.extend.each  @   jquery.js?body=1:359
$.ajax.complete @   new:922
jQuery.Callbacks.fire   @   jquery.js?body=1:3120
jQuery.Callbacks.self.fireWith  @   jquery.js?body=1:3232
done    @   jquery.js?body=1:9291
jQuery.ajaxTransport.send.callback  @   jquery.js?body=1:9686


isArraylike @   jquery.js?body=1:584

584:  typeof length === "number" && length > 0 && ( length - 1 ) in obj;

这是我的错误:

        complete: function(response) {
            response = response.responseText;

如果我手动设置对阵列的响应:

 response = [["Ford","ford"],["Chevy", "chevy"]]

控制器:

  def make
    models = CarDetails.get_makes(params["year"])
    # models is an array that looks like: [["Ford","ford"],["Chevy", "chevy"]]
    render :text => models
  end

功能:

function getMakes(id, year) {
   var makes_id = id.replace("year", "make");
    $.ajax({
        type: "GET",
        url: "/car/make",
        data: {
            'year': year
        },
        complete: function(response) {
            response = response.responseText;
            $("#" + makes_id).empty();
            $.each(response, function(index, value) {
               $("#" + makes_id).append("<option value='" + value[1] + "'>"+ value[0] +"<\/option>")
            });
        }
    });
}

0 个答案:

没有答案