samedi 27 juin 2015

Why is my AJAX request failing?

I'm trying to learn to use AJAX with Rails.

Here is my client side coffeescript code:

$(document).ready ->
  $("#url").blur ->
    $.get("/test_url?url=" + $(this).val(), (data) ->
      alert("Response code: " + data)
    ).fail( () ->
      alert("Why am I failing?")
    )

Here is my server-side Ruby code:

  def url_response
    url = URI.parse(params[:url])
    Net::HTTP.get_response(url).code unless url.port.nil?
  end

The Ruby code is being called and correctly returns the HTTP response code, but I can't do anything with the data because the client-side script says the call has failed. As far as I can see, it is not failing. url_response is being called and it is returning a value, so what exactly is failing here?

Aucun commentaire:

Enregistrer un commentaire