純用 javascript post form
javascript html -postJSON('http://domain/path', {a:1, b:2})
How
const createElement = (node, attrs) => {
const el = document.createElement(node)
const setAttr = (attr) => el.setAttribute.call(el, ...attr)
Object.entries(attrs).forEach(setAttr)
return el
}
const createForm = (url) => createElement('form', {
action: url,
method: 'POST',
style: 'display: none'
})
const createField = (name, value) => createElement('input', {
type: 'hidden',
name: name,
value: value
})
const postJSON = (url, obj) => {
const form = createForm(url)
Object.entries(obj).forEach(([name, value])=> {
if (!Array.isArray(value)) {
value = [value]
}
value.forEach((val)=>{
form.appendChild(createField(name, val))
})
})
document.body.appendChild(form)
form.submit()
document.body.removeChild(form)
}