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


Hi,

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

https://nishantrana.wordpress.com/2007/10/18/calling-aspnet-webservice-from-javascript-ajax/

1) First we will modify the default Hello World service to accept a parameter like this

[WebMethod]

public string HelloWorld(string Name)

{

return “Hello “+Name+”!”;

}

2) Add the html textbox control in the webform

<INPUT type=”text” id=”Info”>

3) Add the following in the body

<BODY onload=”getMessage()”>

4) Put the following script in our webpage.

<script language=”javascript”>

var xmlHttp;

function getMessage()

{

xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);

xmlHttp.open(“get”, “http://localhost/WebService1/Service1.asmx/HelloWorld? name=’Nishant'”, true);

xmlHttp.onreadystatechange=doUpdate;

xmlHttp.send(); return false;

}

function doUpdate()

{

if(xmlHttp.readyState==4)

{

var xmlDoc=xmlHttp.responseXML;

var responseElement=xmlDoc.getElementsByTagName(“string”)[0];

var respText=responseElement.firstChild.nodeValue;

document.forms[0].elements[‘Info’].value=respText;

}

}

</script>

5) We have changed the method of passing the data to server from post to get as we are passing the value for Name by appending it in the url.

6) Right now if we run the application it will give us error as “object not found”.

7) The reason for the error is that we have to make our web service configured for get method.

8- We need to open web.config of our web service and add the following line inside system.web section

<webServices>

<protocols>

<add name=”HttpGet”/>

</protocols>

</webServices>

9) We need to build the service again

10) This time our application should work without any error.

Bye

Author: Nishant Rana

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

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

  1. Hello! thank u very much.
    I have a question about above article:
    i implement just like above code and it works fine in IE, but in FireFox not even responding anything through breakpoint in javascrip function!!
    is there any solution for this code to work in firefox too?

    Like

  2. Hi Sherry,

    xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”)

    This line of code creates the XMLHttpRequest object. This object sends request to the server and processes the responses from it.

    The above code creates the object specific to Internet Explorer( <=6.o).

    It is implemented as Active X for IE. However in IE 7 XMLHttpRequest will come as native JavaScript object.

    For other browsers we can write

    xmlHttp=new XMLHttpRequest();

    or best we can write this

    if(window.ActiveXObject)

    {

    xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);

    }

    else if (window.XmlHttpRequest)

    {

    xmlHttp=new XMLHttpRequest();

    }

    I hope this helps !!

    Like

  3. hello nishantrana,

    the following code is not working in mozilla.
    can u please resolve the problem

    xmlHttp=new XMLHttpRequest();

    or best we can write this

    if(window.ActiveXObject)

    {

    xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);

    }

    else if (window.XmlHttpRequest)

    {

    xmlHttp=new XMLHttpRequest();

    }

    Like

  4. hi nishant,

    sorry to bother you once again.but i have one problem ,if u can please sort it out.

    my prob is that if i m running my insert code in IE,it is working properly but if i m running it in firefox its nt working.in firefox by default the status is taken as o instead of 4.so how can i solve this problem.

    Like

  5. Hi There Nishant,

    im new to this web services..

    and thanx to you, now i’ve managed to call the web service thru JS…

    but there’s question that i want to ask you..

    can the JS pass the array of data to the web services?

    and on retrieving it back to the client side (on the JS), in array of data?

    Thanx in advance…

    Like

  6. hi..
    above mentioned code regarding retrieving data from the webservice through xml is fine.
    But im having problem regarding the execution of that code. after rebuilding the webservice, if i run it. It is showing “Microsoft JScript runtime error: Object required” error. I am unable to get through it. I am waiting for ur reply..

    Like

Please share your thoughts

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

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

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

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

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

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: