Calling Asp.NET web service from javascript (Ajax)-(Passing Parameter-POST)


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”);“POST“, “http://localhost/WebService1/Service1.asmx/HelloWorld”, true);




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


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


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 ->


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

24 thoughts on “Calling Asp.NET web service from javascript (Ajax)-(Passing Parameter-POST)”

  1. 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.


  2. 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


  3. 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?


  4. 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.


  5. 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?


  6. hi nishant.this is quit ehelpful but if i want to insert multiple parameters in the database through javascript how should i do it?


    1. 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) );


    1. Hi Vidhi,

      Suppose this is your web service for updating values in database

      public void UpdateDBService(string firstName,string lastName)
      using (SqlConnection conn = new SqlConnection())
      conn.ConnectionString = “connString”;

      SqlCommand myCmd = new SqlCommand();
      myCmd.Connection = conn;
      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”);“POST”, “http://localhost/Service1.asmx/UpdateDBService”, false);
      xmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
      xmlHttp.send(“firstName=” + escape(firstName) + “&lastName=” + escape(lastName));


  7. 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.



    1. 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

      Nishant Rana


  8. 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.



Please share your thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform


Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions


Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM


Mail to for queries and suggestions

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM


Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts


developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: