Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,im new to javascript's xmlhttprequest so i need some clarification regarding POST method in xmlhttprequest.

if i pass some values using POST method in xmlhttprequest then how can i get posted value in destinated url?if i have code like this,

var xhr = new XMLHttpRequest();
var parameters = "name=XXX"+"& Age=20";
xhr.open("POST","page_2.aspx",true);

/* code here for onreadystatechange event for checking state and status of http*/

xhr.send(parameters);

After all, how can i receive the posted parameters in page_2.aspx???

Thanks in Advance...
Posted

1 solution

Please see below code:
In Javascript
JavaScript
function CallAJAX()
{
	var xhr = new XMLHttpRequest();
	var parameters = "name=XXX"+"& Age=20";
	xhr.open("POST","page_2.aspx",true);

	xhr.send(parameters);
	if (xhr.readyState==4) // 4 = "Response loaded"
	{
		if (xhr.status==200) // 200 = Response Error Free  
		{             
		  alert(xhr.responseText);
		}
		else
		{
			alert("Problem retrieving XML data");
		}
	}
}

In Code-Behind(Page2.apsx)
C#
protected void Page_Load(object sender, EventArgs e)
{
        Response.Cache.SetCacheability(HttpCacheability.NoCache);     
        if (Request.Form["name"] != null)
        {
            string temp = Request.Form["name"];
            Response.Write(DateTime.Now.ToShortTimeString());
            Response.End();
        }
}
 
Share this answer
 
v6
Comments
Maniraj.M 17-Nov-15 6:45am    
That's fine but i asked about receiving parameter in Page_2.aspx...After xhr.send(parameters); how should i get thats parameters in page_2?
Maniraj.M 17-Nov-15 7:06am    
no it will not...Query string applies only if passing values via URL...
F-ES Sitecore 17-Nov-15 7:08am    
If you POST the data then use Request.Form["name"] and Request.Form["Age"]
[no name] 17-Nov-15 7:12am    
Answer updated..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900