0

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?

2
  • Does the ajax call reach your server properly? Commented Jul 30, 2012 at 21:49
  • Also considering that get and post are complete different I'd assume you don't have the proper routes for POST. try $.ajax({..., type: 'GET',...}) Commented Jul 30, 2012 at 21:56

3 Answers 3

1

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. :)

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

Comments

1

The solution was to add:

dataType: "script"

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.