Hi,
Now we will modify the application that we developed in the previous post to work with parameters
http://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
RSS - Posts

[...] You can read the rest of this blog post by going to the original source, here [...]
By: Calling Asp.NET web service from javascript (Ajax)-(Passing Parameter) on October 22, 2007
at 7:14 am
I just wanted to drop a note to say thanks. This worked like a charm.
By: Brian Lee on October 23, 2007
at 5:04 pm
Thanks Brian for such a nice comment.
By: nishantrana on October 24, 2007
at 6:26 am
great! just what i need!
By: cesar! on December 21, 2007
at 11:01 pm
Thanks this is amazing wonderful no words to admire
By: navdeep on September 23, 2008
at 8:26 am
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?
By: sherry on October 13, 2008
at 7:57 am
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 !!
By: Nishant Rana on October 13, 2008
at 9:48 am
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();
}
By: Anonymous on October 31, 2008
at 9:43 am
set your object like this:
if(window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else if(window.ActiveXObject)
xmlhttp=new ActiveXObject(“MSXML2.XMLHTTP.3.0″);
By: Jonathan Schroeder on August 24, 2009
at 7:40 pm