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


Hi,

Now we will modify the application that we developed in the previous post to work with POST parameter

https://nishantrana.wordpress.com/2007/10/22/calling-aspnet-web-service-from-javascript-ajax-passing-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.

Unknown's avatar

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

    Like

  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

    Like

  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?

    Like

  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.

    Like

  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?

    Like

  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?

    Like

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

      Like

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

      Like

  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.

    Cheers,
    Syed.

    Like

    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
      updateRecord(fname,lname);

      Regards,
      Nishant Rana

      Like

  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.

    Regards,
    Syed.

    Like

Leave a reply to Syed Nadeem Cancel reply

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

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

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

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

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

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

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

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.