Ajax响应和json

I am trying to make an ajax call to the controller which inturn calls a method from the model and returns to me an array of values. But I am unable to get any response in ajax. I need to set value of a text field depending upon the response data.

My ajax code is written in .coffee.js:

$.ajax({
        url: '/addresses/billing_address_to_string',
        type: "POST"
        dataType: "JSON"
        success: (data) ->
          $('#billing_address_address_line1').val(data)
      }).done

In my controller:

respond_to :json, only: [:billing_address_to_string]
def billing_address_to_string
    address = Address.last.billing_address_to_string1
    respond_with address
  end

The model method is:

def billing_address_to_string1
    address = []
    address << [name, street, street_qualifier].reject(&:blank?)
    address << [city, state_or_region, postal_code].reject(&:blank?)
    address << [phone_number]
  end

Any help would be much appreciated:)

Does Address.last.billing_address_to_string1 return a String? If so you need to wrap it in a hash to JSON encode it.

Try

 respond_with({:address => address})

Then the JS should be

 $.ajax({
    url: '/addresses/billing_address_to_string',
    type: "POST"
    dataType: "JSON"
    success: (data) ->
      $('#billing_address_address_line1').val(data.address)
  })