定义一个函数
function requestPOST(url, data, cb) {
const iframe = document.createElement('iframe')
iframe.name = 'iframePost'
iframe.style.display = 'none'
document.body.appendChild(iframe)
const form = document.createElement('form')
// input用来存放post提交的数据
const input = document.createElement('input')
iframe.addEventListener('load', function () {
if (cb && typeof cb == 'function') {
cb()
}
})
form.action = url
form.target = iframe.name
form.method = 'post'
Object.keys(data).forEach(key => {
input.name = key
input.value = data[key].toString()
form.appendChild(input.cloneNode())
})
form.style.display = 'none'
document.body.appendChild(form)
form.submit()
document.body.removeChild(form)
}
调用
requestPOST('https://ocpc.baidu.com/ocpcapi/api/uploadConvertData', {
data: data
}, function (response) {
console.log('response', response)
})