tags:

views:

12

answers:

1

I have a piece of Javascript code that uses an XHR to POST data to a URL, but it uses FormData, which is not supported in earlier versions of Chrome. This is very handy, but I want it to be backwards-compatible, so what is the best way to rewrite the makeFormData method to return the data ready to send?

The offending code is here:

http://bitbucket.org/piranha/historious.crx/src/tip/background.html#cl-86

Thank you!

+1  A: 

EDIT: You can override FormData to create an encoded POST string like this...

function FormData() {
  var obj = {}
  this.append = function(key, val) {
    obj[key] = val;
  }
  this.toString = function() {
    var s = "";
    for(var k in obj) {
      s += ((s.length == 0) ? "?" : "&");
      s += k;
      s += "=";
      s += encodeURIComponent(obj[k]);  // might want to use escape() instead
    }
    return s;
  }
}
Josh Stodola
Well, I want to replace it altogether, so I don't need to check... FormData basically converts a dictionary into a URLencoded string.
Stavros Korokithakis
@Stavros Answer updated
Josh Stodola
Thank you for that, that's exactly what I needed!
Stavros Korokithakis