Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does html write post requests

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"html如何写post请求",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"html如何写post请求"吧!

1.为什么要用post请求?

因为在我们使用post请求的时候用户在涉及有关于密码和隐私的内容时,不会在地址栏上面看到我们的内容起到了保护作用,所以这就是为什么要使用post请求的原因。

2.方法一:

我们通过使用传统的方式来对浏览器进行post请求,代码如下:

var postData = { "name1": "value1", "name2": "value2"};postData = (function(obj){ // 转成post需要的字符串. var str = ""; for(var prop in obj){ str += prop + "=" + obj[prop] + "&" } return str;})(postData);var xhr = new XMLHttpRequest();xhr.open("POST", "../module", true);xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");xhr.onreadystatechange = function(){ var XMLHttpReq = xhr; if (XMLHttpReq.readyState == 4&&XMLHttpReq.status == 200) { var text = XMLHttpReq.responseText; console.log(text); }};xhr.send(postData);

3.方法二:

采用虚拟表单进行提交post请求,代码如下:

function post(URL, PARAMS) { var temp = document.createElement("form"); temp.action = URL; temp.method = "post"; temp.style.display = "none"; for (var x in PARAMS) { var opt = document.createElement("textarea"); opt.name = x; opt.value = PARAMS[x]; // alert(opt.name) temp.appendChild(opt); } document.body.appendChild(temp); temp.submit(); return temp; } //调用方法 如 post('pages/statisticsJsp/excel.action', {html :prnhtml,cm1:'sdsddsd',cm2:'haha'});到此,相信大家对"html如何写post请求"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report