JS使用post提交的两种方式

前端之家收集整理的这篇文章主要介绍了JS使用post提交的两种方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了JS使用post提交的两种方式。分享给大家供大家参考,具体如下:

第一种提交post的方式是传统方式,判断浏览器进行post请求。

var xmlobj; //定义XMLHttpRequest对象 function CreateXMLHttpRequest() { if(window.ActiveXObject) //如果当前浏览器支持Active Xobject,则创建ActiveXObject对象 { //xmlobj = new ActiveXObject("Microsoft.XMLHTTP"); try { xmlobj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlobj = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlobj = false; } } } else if(window.XMLHttpRequest) //如果当前浏览器支持XMLHttp Request,则创建XMLHttpRequest对象 { xmlobj = new XMLHttpRequest(); } } function SubmitArticle(act,cityname,antique) //主程序函数 { CreateXMLHttpRequest(); //创建对象 //var parm = "act=firstweather" ;//构造URL参数 //antique = escape(antique); var parm = "act=" + act + "&cityname=" + cityname + "&antique=" + antique;//构造URL参数 //xmlobj.open("POST","{dede:global.cfg_templeturl/}/../include/weather.PHP",true); //调用weather.PHP xmlobj.open("POST","/weather/include/weather.PHP",true); //调用weather.PHP xmlobj.setRequestHeader("cache-control","no-cache"); xmlobj.setRequestHeader("contentType","text/html;charset=uft-8") //指定发送的编码 xmlobj.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); //设置请求头信息 xmlobj.onreadystatechange = StatHandler; //判断URL调用的状态值并处理 xmlobj.send(parm); //设置为发送给服务器数据 }

第二种方式则是虚拟表单的形式提交post请求

调用方法 如:

代码如下:

希望本文所述对大家JavaScript程序设计有所帮助。

原文链接:https://www.f2er.com/js/51308.html

猜你在找的JavaScript相关文章