Is it possible to call a handler using javascript code? e.g. i have a handler deployed at this location http://mysitename.com/getMyData.ashx. Can I call this handler or just request it using javascript? Is it even possible or not? Please suggest.
4 Answers
yes you can
use ajax or jquery ajaxcall for this.
same ajax function :
function showHint(elementid,url,str) {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById(elementid).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url+str,true);
xmlhttp.send();
}
9 Comments
ria
what should i put in the str variable ??? i have put the url mysitename.com/getMyData.ashx in the url variable.
Pranay Rana
you can pass emptystring if you dont want to pass any query string data
ria
actually i did pass an empty string but i thnk this statement is not being executed: document.getElementById(elementid).innerHTML=xmlhttp.responseText; I even placed an alert statement here which didnt work. What could be the issue?
Pranay Rana
check you handler code is it writing data using this statement : HttpContext.Current.Response.Write to write data in ProcessRequest function everything else is fine
Pranay Rana
also check for the elementid i.e element with this id exists or not
|
First of please elaborate a bit what are you trying to do.
You can call it with AJAX and request the webservice URL.
1 Comment
ria
thank you ... actually i didnt ever do it earlier so i wanted a sample code segment :)