javascriptcouchdb

new FormData() "application/x-www-form-urlencoded"


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

xhr.open('put',document.myForm.action,false)
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr.send(new FormData(document.myForm))

Solution

  • Here's a simpler way to do it that doesn't rely on writing your own conversions:

     const form = document.getElementById('my_form')
     fetch(form.action,
           { body: new URLSearchParams(new FormData(form)) })
    

    It uses the fact that the URLSearchParams constructor can accept a FormData object (anything that will iterate pairs of values, I think) and that fetch knows to convert URLSearchParams into a string and set the Content-Type header correctly.