遇到一个在新页面中打开第三方网站,但是只提供了POST请求的需求。
首先要吐槽第三方网站只提供POST请求,简直****!
1、接口测试
首先用postman测试接口,返回html文档,第三方接口用POST请求,默认对方返回的是静态页面,将结果写入新窗口即可;代码测试发现该html文档是第三方系统内部访问的页面,有很多该系统内部的请求。
2、解决方案
表单可以用POST请求向第三方网站提交数据,尝试js内部构造form表单,并提交
const url = '{address}/index_new.php/Login';
const tempForm = document.createElement('form');
tempForm.action = url;
tempForm.target = '_blank';
tempForm.method = 'post';
tempForm.style.display = 'none';
for (const x in argument) {
const opt = document.createElement('input');
opt.type = 'hidden';
opt.name = x;
opt.value = argument[x];
tempForm.append(opt);
}
document.body.appendChild(tempForm);
tempForm.submit();
document.body.removeChild(tempForm);
成功打开新窗口,docum