将“enctype”属性设置为“application / json”
这是我发出POST请求的代码:
function post(path, params, method) { method = method || "post"; // Set method to post by default if not specified. // The rest of this code assumes you are not using a library. // It can be made less wordy if you use one. var form = document.createElement("form"); form.setAttribute("method", method); form.setAttribute("action", path); form.setAttribute("enctype", "application/json"); for(var key in params) { if(params.hasOwnProperty(key)) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", key); hiddenField.setAttribute("value", params[key]); form.appendChild(hiddenField); } } document.body.appendChild(form); form.submit(); }
我尝试通过将表单的enctype
设置为“application / json”将HTTP标头中的Content-type
设置为“application / json”。 但是,它不起作用。
我看到一个关于支持enctype
“application / json”的非正式草案 ,但似乎还没有被接受..
有没有人有关于如何发出POST请求并使用JSON
而不是formdata
作为数据格式而不诉诸AJAX的formdata
?
有没有人有关于如何发出POST请求并使用JSON而不是formdata作为数据格式而不诉诸AJAX的想法?
在当前的浏览器中无法做到这一点。
如果您正在编写需要正常表单提交的HTTP端点,请将其写入以便接受application/x-www-form-urlencoded
和multipart/form-data
编码数据。