Tagged: jQuery

jQuery ajax request yields statusText: No Transport

If you write code you hate Internet Explorer. You get everything just right in Chrome, Firefox, Safari - any other browser really and then everything falls apart in Internet Explorer.

We use CORS in some of the work with do at Open Tapestry. It works really well for letting the client figure out which service it needs to call and then call that system directly rather than routing all the requests through the main site.

At some point someone at Microsoft decided that supporting standards just wasn't for them and so they created the XDomainRequest object. (I find it amusing that the top of the document states "There are no standards that apply here." That attitude permeates Internet Explorer). Lucky for us someone wrote a jQuery ajax transport that talks jQuery into kind of supporting CORS like behavior. It helps but doesn't solve all problems.

When we deployed our code we found that nothing worked in Internet Explorer. No errors. No output. Just nothing.

Here's wha...

Continue Reading →

jurlp and Internet Explorer 8

For those who don't write code the choice to use Internet Explorer probably hasn't been influenced by much more than the fact that it's sitting on the desktop ready to use. For those of us who do write code we hate you.

We use jurlp in quite a bit of our code because it's a great way to break apart and modify urls client side. Today, while testing in IE 8 we started seeing 'Invalid Argument'. I think that's the error of choice for most of Windows and as it turns out it's a pretty worthless/meaningless error.

We traced down into the code and found that jurlp has code that looks like this:

return $.extend ( urlToObject ( url ), { toString : uri.toString.http } );

That is an elegant line of code and shows how things should be done. However, Internet Explorer doesn't like it when you override toString that way. Both jQuery and Underscore.js have outstanding bugs detailing the problem.

We created a fork of jurlp that you can find here that uglies up the c...

Continue Reading →

Require.js, Backbone.js and Almond.js Example Application

We refactored OERGlue to use backbone.js. That makes our life a lot simpler. Along the way we discovered require.js and I entered nerdvana. The documentation for require.js and backbone.js are both good and it doesn't take to long to read through the code. However, putting them together, getting the development environment right and then deploying can be a bit of a challenge.

James Burke was kind enough to guide me through getting my project setup. In hopes that my experience will help someone else get started I've put together an example application that contains the basics that you need to get started:

https://github.com/jbasdf/requirejs-backbone-example

Merry Christmas!

Continue Reading →

Require.js, Backbone.js and Almond.js Example Application

We refactored OERGlue to use backbone.js. That makes our life a lot simpler. Along the way we discovered require.js and I entered nerdvana. The documentation for require.js and backbone.js are both good and it doesn't take to long to read through the code. However, putting them together, getting the development environment right and then deploying can be a bit of a challenge.

James Burke was kind enough to guide me through getting my project setup. In hopes that my experience will help someone else get started I've put together an example application that contains the basics that you need to get started:

https://github.com/jbasdf/requirejs-backbone-example

Merry Christmas!

Continue Reading →

Pushing my luck with jQuery. Scope matters.

OER Glue makes heavy use of jQuery. We're currently working on a proxy version of the technology that won't require installing a browser plugin. It's pretty cool.

One of the biggest challenges for us has been ensuring consistent look, feel and operation of our technology across any possible website on the Internet. That's not a small challenge. A key to making everything work is to not stomp on anything the web page is already doing. We use jQuery and so do a lot of page on the Web. That means we have to carefully load the version that we use (since lots of sites use old versions) and then put everything back. A call to noConflict make this possible:


oerglue_jquery = window.jQuery.noConflict(true);

That magic doesn't come without costs. We don't use any jQuery plugins because it's not likely they will work with our scoped version of jQuery. I also ran into an interested problem with class selectors. For some reason jQuery class selectors for the oerglue_jq...

Continue Reading →

Get jQuery Working With Ruby on Rails 3

I thought I had fixed everything and made ready for Rails 3 in my applications. Turns out I was wrong. My wife tells me I'm wrong all the time so I didn't feel to bad. The first sign was that something bad had happened was that all my delete links stopped working. I found a couple of articles talking about the new unobtrusive javascript in Rails.

I have bits of code that look like this:


<%= link_to 'Delete Thing', @thing, :confirm => 'Are you sure?', :method => :delete %>

I know the argument that you shouldn't use links for delete. In this case the generated anchor tag won't cause you problems so I don't want to hear any whining:


Delete Course

I included rails.js which will turn those links into DELETE requests for the server. However, I started seeing this error in Firebug on Firefox:


document.on is not a function

I also got this in Google Chrome:


Uncaught TypeError: Object # has no method 'on'

Turns out that since I use...

Continue Reading →

Get jQuery Working With Ruby on Rails 3

I thought I had fixed everything and made ready for Rails 3 in my applications. Turns out I was wrong. My wife tells me I'm wrong all the time so I didn't feel to bad. The first sign was that something bad had happened was that all my delete links stopped working. I found a couple of articles talking about the new unobtrusive javascript in Rails.

I have bits of code that look like this:


<%= link_to 'Delete Thing', @thing, :confirm => 'Are you sure?', :method => :delete %>

I know the argument that you shouldn't use links for delete. In this case the generated anchor tag won't cause you problems so I don't want to hear any whining:


Delete Course

I included rails.js which will turn those links into DELETE requests for the server. However, I started seeing this error in Firebug on Firefox:


document.on is not a function

I also got this in Google Chrome:


Uncaught TypeError: Object # has no method 'on'

Turns out that since I use...

Continue Reading →

Converting a jQuery Object into a String

I'm so used to everything having a 'to_s' method that when it's not there I feel like going out a kicking puppies. In jQuery you can always get the contents of a jQuery object thus:

Html:


var html = myObject.html();

Text:


var html = myObject.text();

That's great and has been extremely helpful over the years but what if you want the html of the object as well? It turns out that there are others that have had this problem. Here's the solution on stackoverflow.com. There's a request for the jQuery team to implement an outerHtml() method. Go add a comment and push the request, please.

I was using the solution from stackoverflow and it worked well until one day all of the contents of my page vanished upon execution of a method leaving me with a serious WTF at 1 am in the morning.

Here's part of my code. I've found that for manipulating an html string jQuery is the bomb. In the code below I take a snippet of html, remove whitespace and then rewrite li...

Continue Reading →

Uploadify onComplete Wouldn’t Fire Until I Swore – A Lot

I've been trying to get Uploadify to work inside my uploader gem. Things went pretty well until I combined that with the muck-contents gem and tried to do multiple file uploads from within a plugin I'd written for the TinyMCE editor. At that point things got ugly.

If you do much research on Uploadify you'll find that it's pretty common to hear someone complain that the OnComplete event does not fire. There's a number of solutions that people have come up with, but mostly they sound like voodoo - Be sure to only return a '1', make sure to use an id instead of a css class, etc. I tried everything I read about and finally got mad enough to debug the fla file.

If you are that desperate make sure to get FireFlash. It will let you write to the Firebug console from within your swf file. It's very handy when you need to see what's going on inside your Actionscript while running inside the browser. I creat...

Continue Reading →

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

Continue Reading →

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

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 met...

Continue Reading →

Hierarchies, trees, jQuery, Prototype, script.aculo.us and acts_as_nested_set

'single_message' should normally be set to false. I added it in just in case I needed to render a single message for an ajax call. If you aren't rendering an entire tree and thus have only one node then passing 'single_message = true' will force the method to call the database to get the level of the node in the tree.

If you want to render a true tree structure (not just indents) then you'll need to do a bit of recursion. Assuming @message is a root level message you can do this:

    <% render_messages(@message) do |message| -%> <%= message.text %> <% end -%>


module MessagesHelper

def rendermessages(message, &block) concat('<li id="message' + message.id.tos + '" class="messageContainer delete-container">', block.binding) yield(message) concat('<ul style="display:none;" id="ul' + message.dom_id + '">', block.binding) if has_chi...

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 →