Forum: Ruby on Rails Rails 3.2 jQuery JSONP request not working - Moved Permanently

Posted by Kad Kerforn (kadoudal)
on 2013-01-16 23:42
(Received via mailing list)
I am trying to send a POST request ( 'remove_tag_named' )and getting a
simple OK back on success ; it doesn't work..
the post is correctly sent , but after then it's looping on sending a 
GET
request to the same url, with the error
301 Moved Permanently

I should not be far fro the solution, but I am stuck :

In my routes.rb
      *namespace :administration* do
        *resource :dashboards*, :only => :show do
          collection do
            *post :remove_tag_named*
          end
        end
      end

In my view :
*= link_to t(:delete), "#", :id => :delete_tag_link*
     #   generated  html =>   <li><a id="delete_tag_link"
href="#">remove</a></li>

my js.coffee  script (  # this should be using JSONP I guess )
  $('*#delete_tag_link*').bind "click", (e) ->
    selected = $('#tags.tab-pane.active #tag-list li 
span.myTag.selected')
    tagName = selected.html()
    alert 'going remove tag: ' + tagName   # checking I get the correct 
tag
value
    $.ajax
      url: "/" + locale + 
"/administration/dashboards/remove_tag_named.json"
      type: "POST"
      dataType: "json"
      contentType: "json"
      data: {"tag_name": tagName }
      success: (result) ->
        selected.remove()

I have also set an init js

  $.ajaxSetup beforeSend: (xhr) ->
    xhr.setRequestHeader "X-CSRF-Token",
$("meta[name=\"csrf-token\"]").attr("content")


and the console output :
[23:18:24.424] POST
http://localhost:3000/fr/administration/dashboards...
[HTTP/1.1 301 Moved Permanently 46ms]
[23:18:24.491] GET
http://localhost:3000/fr/administration%2Fdashboar...
[HTTP/1.1 301 Moved Permanently 34ms]
[23:18:24.547] GET
http://localhost:3000/fr/administration%2Fdashboar...
[HTTP/1.1 301 Moved Permanently 35ms]
[23:18:24.550] GET
http://localhost:3000/fr/administration%2Fdashboar...
[HTTP/1.1 301 Moved Permanently 48ms]
.........  more GET......
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.