转载自:http://blog.csdn.net/wzwlln/article/details/6186520
版权声明:本文为博主原创文章,未经博主允许不得转载。
一般都是写上隐藏的form标签,用<a href="#" οnclick="..."/>来调用js函数然后submit
全部用js来写也行,以下是我在一个问答频道看见别人写的例子,放在这里
- 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'});
1. [代码]原生JS实现ajax 发送post请求 跳至 [1] [全屏预览]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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) {
if
(XMLHttpReq.status == 200) {
var
text = XMLHttpReq.responseText;
console.log(text);
}
}
};
xhr.send(postData);
|