I am trying to hardwire an on-the-fly created form to send JSON using the encoding type “application/json“.
in jQuery, I would set this as ‘contentType‘ in an $.ajax or a $.post – however for certain reasons, I need to be doing this manually.
I have the following code, but it just doesn’t work. It still defaults the enctype to application/x-www-form-urlencoded
data = data;
var form = document.createElement("FORM");
form.style.display = "none";
form.action = url;
form.setAttribute('enctype', 'application/json');
form.method = "post";
Am I trying to set the wrong property, or am I just setting it wrong? Any ideas?
I don’t think a
formcan do that.You’ll need to do it on the server side. Or if you must do it on client prior to sending (not recommended) then look at a JSON library.