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.
+2
A:
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();
}
Pranay Rana
2010-05-19 08:21:13
what should i put in the str variable ??? i have put the url http://mysitename.com/getMyData.ashx in the url variable.
ria
2010-05-19 10:18:34
you can pass emptystring if you dont want to pass any query string data
Pranay Rana
2010-05-19 10:19:55
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?
ria
2010-05-19 10:56:09
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
2010-05-19 11:10:42
also check for the elementid i.e element with this id exists or not
Pranay Rana
2010-05-19 11:11:13
the element with the id exists, and the handler is also writing data using the statement context.Response.Write("<html><body><table style='width:100%;'> ")...is it because i m using html in the context.response.write?
ria
2010-05-19 12:27:45
have no idea about this but in my application i am just returning url i.e writing url form the handler
Pranay Rana
2010-05-19 12:40:39
ok one question ... if i do pass some querystring parameters in the str variable ... it shd be in this format str = "?abc=xyz";so tht when the url + str is executed it becomes "http://mysitename.com/myhandler.ashx?abc=xyz" ... right? basically i m asking we do have to use the question mark to identify the querystrings from the url... sorry its a basic question bt since u jst concatenated both the variables i was thinking why dont we use the complete url (with the querystring parameters) in jst one variable i.e. url...
ria
2010-05-20 06:36:40
yes its correct you can pass query string data in str parameter str = "?abc=xyz";
Pranay Rana
2010-05-20 06:54:30
+1
A:
You can use XMLHttpRequest (AJAX, not necessarily using XML) to load an URL in the background. I'd highly suggest you to do it through a javascript framework like jQuery since that saves you from accessing the ugly low-level interface directly.
ThiefMaster
2010-05-19 08:21:34