window.open()用post传递参数

 
 
做一个小东西,就是简单的点击生成文件按钮,然后通过后台生成文件后传递到前台展示。因为不想用get方式请求,
毕竟把一堆信息挂在网页上呵呵。

然后初步的想法就是ajax,没错这是一个好东西,和我预期的结果一样,也实现了想要的功能,但是就是它返回了一个文件,

然后再open,感觉就很多余。

然后尝试着可不可以直接open,用post方式请求

百度之后:


ow.open() post请求
function exportExcel(cur){
    var tempForm = document.createElement("form");  创建一个form表单
    tempForm.id = "tempForm1";  form的id
    tempForm.method = "post";  请求方式为post
    tempForm.action = '{% url "downloadExcel" %}'; 这里是跳转的页面
    tempForm.target="_blank"; //打开新页面
    var hideInput1 = document.createElement("input"); input输入框
    hideInput1.type = "hidden";  隐藏input
    hideInput1.name="urlflag"; //后台要接受这个参数来取值,传参
    hideInput1.value = window.urlflag; //后台实际取到的值

    var hideInput2 = document.createElement("input");
    hideInput2.type = "hidden";
    hideInput2.name="startTime"; //后台要接受这个参数来取值
    hideInput2.value = startTime; //后台实际取到的值

    var hideInput3 = document.createElement("input");
    hideInput3.type = "hidden";
    hideInput3.name="endTime"; //后台要接受这个参数来取值
    hideInput3.value = endTime; //后台实际取到的值

    tempForm.appendChild(hideInput1) ; //表单里面添加input
    tempForm.appendChild(hideInput2);
    tempForm.appendChild(hideInput3); 
    
    addEventListener()是标准的绑定事件监听函数的方法,是W3C所支持的,Chrome、FireFox、Opera、Safari、IE9.0及其以上版本都支持该函数;但是,IE8.0及其以下版本不支持该方法,它使用attachEvent()来绑定事件监听函数。所以,这种绑定事件的方法必须要处理浏览器兼容问题
 
 
if ( document .all){ document.all是页面内所有元素的一个集合 tempForm .attachEvent( "onsubmit" , function (){}); //IE绑定onsubmit的事件响应 } else { var subObj = tempForm . addEventListener ( "submit" , function (){}, false ); //firefox } document .body. appendChild ( tempForm ); 整个文档里面添加form if ( document .all){ tempForm .fireEvent( "onsubmit" ); } else { tempForm .dispatchEvent( new Event ( "submit" )); } tempForm . submit ();提交事件 document .body. removeChild ( tempForm );提交完成后删除 }

javascript事件触发器fireEvent和dispatchEvent https://www.cnblogs.com/tiger95/p/6962059.html

js document.all 的用法 https://blog.csdn.net/xiaoyu714543065/article/details/38919463

猜你喜欢

转载自blog.csdn.net/goblinM/article/details/80205620
今日推荐