boogdesign posts
30/08/07
Prototype.js Ajax.Request: Beware of carriage returns in response text
Another in the long running saga "Dumb things Rob did with Javascript." I ran across a strange error in some fairly simple Ajax code the other day. Here's the code:
function () {new Ajax.Request(urlTxtSrch+'&txtTEXTSRCH=' + encodeURIComponent( $('txtTEXTSRCH').value),
{onComplete:function(request){try{eval(request.responseText)}
catch(ex) {$('textSearchResults').innerHTML = '<p>Error getting results: ' + ex.message + '</p>'}},
evalScripts:true,
asynchronous:true}); return false;}
Using prototype, I'm taking the value out of a text box and returning some javascript code which writes a list of search results into a div. Not the best way to do it, but it was basically a one off situation so there wasn't much benefit to making it more generic and figuring out how to do without the nasty eval. The search results were limited to twenty items with three bits of info each, the final one of which was the first 200 characters of a 4000 character varchar field we have in our database.
The error I kept getting was malformed string. It didn't happen all the time, in fact not very often at all, so it took me a while to work out that it was particular result items which were causing the problem rather than simply the size of the results. The problem, as many will no doubt already be aware, is that string literals in javascript cannot extend across multiple lines. In fact, I too was aware of this, and my server side code was already replacing vbCrLf (yes, it's VB running on Windows) with <br> so I was a little confused as to why it was happening.
Some people have probably already spotted the dumb assumption I was making. Since I was already truncating the 4000 character field to 200 characters there were some situations where the 200th character was the carriage return part of a carriage return plus line feed combo (the required line ending on Windows). Javascript is quite happy to accept a carriage return by itself as the end of a line where Windows is not.
Trackback address for this post:
http://www.boogdesign.com/b2evo/th1srv/trackback.php/209
Comments, Trackbacks, Pingbacks:
No Comments/Trackbacks/Pingbacks for this post yet...
Leave a comment:
Hot Topics
Popular
boogdesign posts
Longer posts on standards based web design, portable web development and Linux, intermingled with some stuff on my other nerd interests.
Search
Categories
- All
- General (13)
- Linux (42)
- Debian / Ubuntu (5)
- Fedora / Red Hat (17)
- Gentoo (5)
- SuSE (12)
- Semantic Web and Microformats (5)
- Web Design (21)
- Web Develop (40)
Archives
- June 2008 (5)
- May 2008 (3)
- April 2008 (3)
- March 2008 (3)
- November 2007 (2)
- October 2007 (3)
- August 2007 (1)
- July 2007 (2)
- June 2007 (8)
- May 2007 (3)
- April 2007 (2)
- March 2007 (4)
- More...








