Search code examples
javascriptstringreturnashx

How do you call an ASHX from JavaScript?


I want to call an ASHX file and pass some query string variables from JavaScript and get the return string into a string in the JavaScript. How would I do this?

The ASHX file is already coded to response.write a string based on whatever the query strings are.


Solution

  • Something like this?:

    function createXMLHttpRequest() {
       try { return new XMLHttpRequest(); } catch(e) {}
       try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
       try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
       alert("XMLHttpRequest not supported");
       return null;
     }
    
    var xmlHttpReq= createXMLHttpRequest();
    xmlHttpReq.open("GET", "your.ashx?v1=1&v2=2&etc", false);
    xmlHttpReq.send(null);
    var yourJSString = xmlHttpReq.responseText;