Hi,
Now we will modify the application that we developed in the previous post to work with POST parameter
We have changed the get to post as we are now not passing the value for Name by appending it in the url.
Only make changes to the function getMessage()
function getMessage()
{
var name=’Nishant’;
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
xmlHttp.open(“POST“, “http://localhost/WebService1/Service1.asmx/HelloWorld”, true);
xmlHttp.onreadystatechange=doUpdate;
xmlHttp.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);
xmlHttp.send(“name=”+escape(name));
return false;
}
Let’s understand the changes that we have made
First of all we have change the method of passing data to POST from GET.
But still we need to pass the name’s value which our web service method needs.
For that we’ll modify our Send() method
xmlHttp.send(“name=”+escape(name));
We are using the same name/value pair in send() which we used at the end of the request URL in the GET version
escape()-It makes sure that the values are valid values i.e. no tricky characters are there.
But this is not enough that is why we added this line
xmlHttp.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);
Here the server doesn’t know what kind of data it can expect or is coming from our application.
We are making use of setRequestHeader function to tell the server about the content type we are going to send.
The server can get this information from request’s header that it recieves.
Content-Type -> Name of the header
application/x-www-form-urlencoded -> http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
Bye
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.

It works fine when only one parameter is to be passed. But could you guide how to pass multiple parameters, when the parameter value contains a ‘&’?
Reply awaited.
Thanks.
LikeLike
Hi Monali,
Say we have to send two parameters fName and lName so in that case we
will have request.send in the following format
request.send( “fName=”+escape(fName) +
“&lName=”+escape(lName) );
I hope this helps you
LikeLike
This was just what I was searching for. Thanks for the post! Can you tell me why you would choose to use the POST rather than the GET option?
LikeLike
Thanks Holly!!
There is a good explanation given over here for Post vs Get
http://weblogs.asp.net/mschwarz/archive/2006/12/04/post-vs-get.aspx
LikeLike
Thank you!! It helped me to solve my problem 😀
LikeLike
good
LikeLike
Lovely boss. I have been struggling for a while on how to call a WS from JS. This works like a wonder. Thanks for the useful blog.
LikeLike
I have a problem Nishant. This works fine for localhost webservices. But when give any machine name instead of localhost, it gives ‘Permission Denied’ error. Any pointers?
LikeLike
I guess you’ll need to configure it for httpPost
Put the following line of code in you web service’s web.config
or otherwise the problem could be of cross-domain scripting.
LikeLike
Cool. I have put the httppost in the protocols of web.config and it worked.
Thanks a ton.
LikeLike
If I pass parameters in doUpdate then it gives error
LikeLike
hi nishant.this is quit ehelpful but if i want to insert multiple parameters in the database through javascript how should i do it?
LikeLike
In case you need to pass more than one parameter, you could use this.
Suppose you need to pass value for fName and lName.
request.send( “fName=”+escape(fName) +
“&lName=”+escape(lName) );
LikeLike
what should we write in do update method
?
LikeLike
Hi Vidhi,
Suppose this is your web service for updating values in database
[WebMethod]
public void UpdateDBService(string firstName,string lastName)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = “connString”;
SqlCommand myCmd = new SqlCommand();
myCmd.Connection = conn;
conn.Open();
myCmd.CommandText = “Update testtable set lastname='”+firstName+”‘,firstname='”+lastName+”‘ where id=1 “;
int g=myCmd.ExecuteNonQuery();
}
}
For calling this web service from within the JavaScript you would use the below code
function updateRecord() {
var firstName = ‘fn’;
var lastName = ‘ln’;
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
xmlHttp.open(“POST”, “http://localhost/Service1.asmx/UpdateDBService”, false);
xmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
xmlHttp.send(“firstName=” + escape(firstName) + “&lastName=” + escape(lastName));
alert(xmlHttp.responseText);
}
LikeLike
hi Nishant,
thanx a lot.it was jus unbelievable.it worked in one time only.u r a genius.thanx once again
LikeLike
Hi Nishant,
Going by the sample code you’ve written for Nidhi’s requirement, I would like to know if there is a way of getting firstName and lastName values through parameters passed to updateRecord() function rather than hardcoding the values in the function?
That would be of immense help to me.
Cheers,
Syed.
LikeLike
Hi,
The updateRecord() is a simple JavaScript function. You could declare parameter for first name and last name to that function and can pass values for them.
declare your function in the following manner
updateRecord(fname,lname);
Regards,
Nishant Rana
LikeLike
I need to call this method using a Silverlight Function in its Application_Exit() method
private void CallJSFunction(object sender, EventArgs e)
{
HtmlPage.Window.Invoke(“updateRecord”, fname,lname);
}
Is that something you can help me with? I am not sure if you are into Silverlight by any chance.
Regards,
Syed.
LikeLike
Hiii Nishant
Gr8 post,
Can you suggest me , how to pass authenticate username and password???
Thanks in advance
LikeLike
Hiiii Nishant,
gr8 post man.
Can you suggest me how to pass authenticate username and password???
Thanks in advance
LikeLike
In keeping with the great Walt Disney, it is curiosity that leads us us
down new paths. I am genuinely curious to walk this path together with you and find exactly where it goes to.
LikeLike
I adore this unique blog. It’s for me a secret haven where I always find something that intrigues me.
LikeLike