new FormData() “application/x-www-form-urlencoded”

后端 未结 3 1665
独厮守ぢ
独厮守ぢ 2020-12-06 01:43

Couchdb only parse application/x-www-form-urlencoded. Is there a FormData() attribute that set the enctype?

xhr.open(\'put\',document.myForm.action,false)
xh         


        
3条回答
  •  情歌与酒
    2020-12-06 02:43

    Some time ago I wrote the following function. It collects form values and encodes them url encoded, so they can be sent with content type application/x-www-form-urlencoded:

    function getURLencodedForm(form)
    {
      var urlEncode = function(data, rfc3986)
      {
        if (typeof rfc3986 === 'undefined') {
          rfc3986 = true;
        }
    
        // Encode value
        data = encodeURIComponent(data);
        data = data.replace(/%20/g, '+');
    
        // RFC 3986 compatibility
        if (rfc3986)
        {
          data = data.replace(/[!'()*]/g, function(c) {
            return '%' + c.charCodeAt(0).toString(16);
          });
        }
    
        return data;
      };
    
      if (typeof form === 'string') {
        form = document.getElementById(form);
      }
    
      var url = [];
      for (var i=0; i < form.elements.length; ++i)
      {
        if (form.elements[i].name != '')
        {
          url.push(urlEncode(form.elements[i].name) + '=' + urlEncode(form.elements[i].value));
        }
      }
    
      return url.join('&');
    }
    
    // Example (you can test & execute this here on this page on stackoverflow)
    var url = getURLencodedForm('post-form');
    alert(url);
    

提交回复
热议问题