views:

315

answers:

3

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
what should i put in the str variable ??? i have put the url http://mysitename.com/getMyData.ashx in the url variable.
ria
you can pass emptystring if you dont want to pass any query string data
Pranay Rana
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
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
Pranay Rana
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
have no idea about this but in my application i am just returning url i.e writing url form the handler
Pranay Rana
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
yes its correct you can pass query string data in str parameter str = "?abc=xyz";
Pranay Rana
+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
A: 

First of please elaborate a bit what are you trying to do.

You can call it with AJAX and request the webservice URL.

fredrik
thank you ... actually i didnt ever do it earlier so i wanted a sample code segment :)
ria