IE7 XmlHttpRequest parseerror problem

browsers ajax debugging ruby rails metal

09|24|2009

Hair Pulling Galore

Have you ever run into the problem in Rails Metal where AJAX Requests just choke? When you try and debug the error response, it would stupidly reply "parseerror".

After a long fought war with the browser that they call IE7, I discovered that this happens due to the Content-Type response header. I played around with different responses, and apparently returning the appropriate responses for my case (which were text/plain and application/json) resulted in IE choking.

Following is the original code I used in my metal application:

    if env["PATH_INFO"] =~ /^\/lookup_hospitals/
      [ 200, { "Content-Type" => "application/json" },
        render_hospitals_dropdown(env)
      ]  
    else
      [ 404, { "Content-Type" => "text/html" }, ["Not Found"] ]
    end

The Solution

I switched to returning a content-type with application/octet-stream, and voila, everything worked as advertised. I hope this doesn't happen to anyone else, although I'm pretty sure it will. Since I had a lot of autocompletion code using rails metal I refactored my code to look like this:

    get env, 'lookup_hospitals' do
      render_hospitals_dropdown(env)
    end

The new code spits something like this

    [ 200, 
      { "Content-Type" => "application/octet-stream", 
        "Content-Length" => result.length.to_s 
      },
      [ result ]
    ]

And surprisingly, IE 6 and IE 7 likes application/octet-stream, which shouldn't be the case.

blog comments powered by Disqus