开发者

How can I rewrite a piece of JS that uses XHR with FormData to be backwards-compatible?

I have a piece of Javascr开发者_如何学Cipt 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!


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;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜