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.

4 Answers 4

6

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();
}
Sign up to request clarification or add additional context in comments.

9 Comments

what should i put in the str variable ??? i have put the url mysitename.com/getMyData.ashx in the url variable.
you can pass emptystring if you dont want to pass any query string data
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?
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
also check for the elementid i.e element with this id exists or not
|
2

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.

Comments

1

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

thank you ... actually i didnt ever do it earlier so i wanted a sample code segment :)
1
$(document).ready(function () {
        saveCookies('true');
    });

function saveCookies(save) {
        $.ajax({
            url: "/Handlers/getMyData.ashx.ashx",
            data: { 'savecookies': save },
           async: false,
            success: function (data, status, xhr) {   
            }
        });
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.