Tagged: ajax

jQuery Ajax requests are ‘html’ not ‘js’

I like jQuery. I use it in all my Rails projects these days. One thing I've struggled with for the longest time is that for some reason all my jQuery ajax requests come through as html not as js.

I thought adding something like this to application.js would fix the problem:

 // In application.js jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} }) 

That won't save you. This change to Rails prevents the Accept header from being used: http://github.com/rails/rails/commit/2f4aaed7b3feb3be787a316fab3144c06bb21a27

The trick to fixing this problem comes from a comment in that commit:

Returns true if the request's "X-Requested-With" header contains

"XMLHttpRequest". (The Prototype Javascript library sends this header with

every Ajax request.)

jQuery doesn't send that parameter with each request. I've written before about modifying the request. http://www.justinball.com/2009/07...

Continue Reading →

Userfly Ruined My Weekend – “jQuery(“form.ajax”).ajaxForm is not a function”

I've been a big fan of Userfly because they make usability testing easy. Their tool is really quite amazing. However, this last weekend and into today I've become less of a fan. After a late night deploy last week on a site I've been working on I noticed that none of my javascript worked. Lucky for me Firefox reports the errors. The errors were annoying things like "jQuery("form.ajax").ajaxForm is not a function". I knew for a fact that jquery.form.js was loaded. I could see the code. On occasion I've had problems with the javascript caching that Rails uses - it merges all the files together. I turned that off and it wasn't it

I could only see the error in production. In development everything worked fine. I swore a lot because that is the worst kind of error. yYou feel like you need to debug on the server or setup a staging server. Running my local box in 'production' mode didn't help. I couldn't reproduce the error until I remem...

Continue Reading →

jQuery.ajax – Get in Firefox, Post in Internet Explorer

The docs for jQuery.ajax indicate that by default the request is made using 'GET'. I would still recommend setting the type to 'GET'. We just deployed a site and found that for some reason some versions of Internet Explorer were doing a POST instead of a GET. This is problematic with a Ruby on Rails site since the same url routes to different locations based on the HTTP verbs. We couldn't figure out why we were getting a bunch of incorrect create requests until we realized the before stated issue.

I suck and I am a liar. I thought that adding GET to the ajax request fixed it but in fact it did not. I spent most of the night going WTF without any LOL while my PPH started going through the roof. The air started to stink from the foul language. No wonders I am losing all my hair.

I finally chose to gird up my loins, turn my back on the Mac, and use my PC. I fired up Visual Studio 2008 attached it to Internet Explorer and prepared a full barrage of colorful metaphors.

A...

Continue Reading →

Ajax calls using jQuery in Ruby on Rails

jQuery is the hot javascript library and it is small and it is cooler than Prototype, the default library that comes with Ruby on Rails. Ergo I plugged it in and pulled out everything else. The ajax calls are very simple and look like this:

 $.post("function_name",{ key:value } ,function(data) { // do stuff with data }); 

The problem is that you will start seeing 'ActionController::InvalidAuthenticityToken' and your ajax calls will fail.

Here is a very elegant solution.

Continue Reading →