Ajax关于get和post请求传递变量的问题
程序员文章站
2022-03-06 12:24:51
...
- GET请求传单个变量
xmlhttp.open("GET","getByNum.php?num="+str,true);
xmlhttp.send();
- GET请求传多个变量
xmlhttp.open("GET","getByNum.php?num="+str+"&sum="+astr,true);
xmlhttp.send();
- POST请求传单个变量
xmlhttp.open("POST","getByNum.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("num="+str);
- POST请求传多个变量
xmlhttp.open("POST","getByNum.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("num="+str+"&sum="+astr);
注意:
如果使用post请求一定要在xmlhttp.open()和xmlhttp.send()之间加上xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");