window.open(url)只能发送get请求,要想发送post请求参数,需要创建表单,然后将参数提交给后台
代码如下:
function openPostWindow(url,data) { // data 是发送请求的参数对象
var tempForm = document.createElement("form");
tempForm.id = "tempForm1";
tempForm.method = "post";
tempForm.action = url;
//tempForm.target = "_blank"; //打开新页面
for(var key in data){
var hideInput = document.createElement("input");
hideInput.type = "hidden";
hideInput.name = key; //参数名
hideInput.value = data[key]; //实际参数值
tempForm.appendChild(hideInput);
}
if (document.all) {
tempForm.attachEvent("onsubmit", function () { }); //IE
} else {
var subObj = tempForm.addEventListener("submit", function () { }, false); //firefox
}
document.body.appendChild(tempForm);
if (document.all) {
tempForm.fireEvent("onsubmit");
} else {
tempForm.dispatchEvent(new Event("submit"));
}
tempForm.submit();
document.body.removeChild(tempForm);
}
注意:url一定要是完整的接口链接,否则会报错,一般公司为了方便更改域名,会封装好接口请求的格式,在请求接口的时候就不用写域名,但这里一定要写完整!!!
或者可以通过获取当前域名,然后进行拼接