Ruby on Rails — $ .ajax不起作用

I know this code works fine, but when I put this code on my (ruby on rails) project it doesn't work.

    <script type="text/javascript">
     $.ajax({
          type: "POST",
          url: "http://www.mywebsite.com",
          data: { name: "John", location: "Boston" }
          }).done(function( msg ) {
          alert( "Data Saved: " + msg );
      });
    </script>

But it works when I replace $.ajax() by $.get()

$.get("/users/20.json", function(data)
      {
      }).done(function( msg ) {
          alert( "Data Saved: " + msg );
       });

Someone knows why on my (ruby on rails) project $.ajax doesn't work but $.get() works fine?

I think you are using jQuery for this. Try replacing your jQuery with the latest version.

The solution was to add:

dataType: "script"

Is your routing restricting the processing to GET requests? That would explain "POST"-based queries (your first example) not working, while "GET" based ones do.

See http://guides.rubyonrails.org/routing.html for info on this...

You could try just changing the "type" option in your $.ajax function to be like this:

   <script type="text/javascript">
 $.ajax({
      type: "GET",
      url: "http://www.mywebsite.com",
      data: { name: "John", location: "Boston" }
      }).done(function( msg ) {
      alert( "Data Saved: " + msg );
  });
</script>

EDIT: Oops, saw you fixed this while I was typing it up. :)