首页 > Web开发 > 详细

为什么Ajax XMLHttpRequest POST方法传递参数失败了

时间:2020-07-05 14:26:16      阅读:101      评论:0      收藏:0      [点我收藏+]

XMLHttpRequest 可以Get,也可以Post向服务器传递参数

var xhr = new XMLHttpRequest();
$(document).ready(function(){
$("button").click(function(){

var paras = ‘firstname=zhang&lastname=san‘;
//Get 方式,直接将参数放到URL链接中
xhr.open("get","God.asp?firstname=zhang&lastname=san&toGod=Gold");
xhr.send(null);


//Post 方式,参数放在单独的请求中
xhr.open("post","God.asp");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
xhr.send(‘firstname=zhang&lastname=san‘);

如果Post方式,需要指定内容的格式:

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");

 

后台使用ASP

<%

dim fn
fn = Request.querystring("firstname")
if (fn = "") then
  fn = Request.Form("firstname")
end if

dim ln
ln = Request.querystring("lastname")
if (ln = "") then
  fn = Request.Form("lastname")
end if

%>

对于Get方式传参, 使用Request.querystring();

而是用Post方式传参,使用Request.Form()

 

当然,POST传递参数,有四种方式,

form-data、x-www-form-urlencoded、raw、binary

有兴趣的同学,可以再研究一下。

 

为什么Ajax XMLHttpRequest POST方法传递参数失败了

原文:https://www.cnblogs.com/montai/p/13245949.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!