0

I am trying to perform a very simple getJson() request but no matter what I change I am getting parseerror from jQuery.

I am using ROR 3.01, Mongoid2.0, jQuery 1.5.2

The Controller Sending the json

    class SuggestController < ApplicationController

      def states
          states = States.only(:name, :state_id).where(:country_code => params[:country])
          render :json => states.entries
      end

    end

Jquery to handle the response.

    $("#property_country").change(function(){

        $.getJSON("/suggest/states",{country: $(this).val(), ajax: 'true'}, function(j){
        var options = '';
        for (var i = 0; i < j.length; i++) {
          options += '<option value="' + j[i].state_id + '">' + j[i].name + '</option>';
        }
        $("select#property_state").html(options);
      });

    });

  $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
        alert(textStatus);
        alert(errorThrown);
        alert(XMLHttpRequest.responseText);
  }});

And example of the json response

[{"_id":"4d925dbe4aa6936d4f0cdefe","name":"Devon","state_id":1},{"_id":"4d925dbe4aa6936d4f0cdeff","name":"Berwickshire","state_id":2}]

I cant seem to find out why im getting parseerror and what it actually means.

Hope you can advise.

1
  • Haven't had any luck reproducing this :/ Does it give you an error if you parse the response manually in the console with $.parseJSON? Commented Apr 2, 2011 at 2:13

1 Answer 1

2

Ok I finally worked it out.

Basically because render => :json already created the objects all I need to do was do a normal get request and then just loop my data.

Hope this helps someone in the future.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.