為什么Ajax XMLHttpRequest POST方法傳遞參數失敗了


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

有興趣的同學,可以再研究一下。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM