1

Please use my previous question as reference. I have marked an answer already.

Question

I'm a .NET developer doing some work for a company that uses Classic ASP. My experience with server side development is VB.NET or C# with some sort of MVC pattern. Consider the following code snippet, I wrote it in an ASP page the company would like to keep and "include" in other pages where this web call would be needed. Kind of like a reusable piece of code. I've left some out for sanity reasons.

//Create ActiveXObject, subscribe to event, and send request
        var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                xmlDoc.loadXML(xmlHttp.responseText);
                debugger;
                var JSON = $.xml2json(xmlDoc);
                parseResponse(JSON);
            }
        }

        urlToSend = encodeURI(REQUEST);
        urlRest += urlToSend
        xmlHttp.open("GET", urlRest, false);
        xmlHttp.send(null);

After struggling with a variety of security problems, I was glad when this finally worked. I changed a setting in Internet Options to allow scripts to access data from other domains. I presented my solution, but the Company would have to change this setting on every machine for my script to work. Not an ideal solution. What can I do to make this run on the server instead of the client's browsers?

I have a little bit of knowledge of the <% %> syntax, but not much.

Community
  • 1
  • 1
user1290284
  • 85
  • 2
  • 12
  • I hope someone finds this useful...The easiest thing for me to do was to create a secondary ASP page responsible for interacting with the web service. If you're using javascript, make sure you specify so. <% @language=javascript %> or something of similar context. Use the <% %> again and put all of your logic there that calls the outside web service and use Response.Write to return the web service response. Whatever is between those brackets is treated like OnLoad, when you call this page that code will be run automatically. In your ASP page you want to consume this information.. – user1290284 May 23 '12 at 00:13
  • Use regular AJAX. $.ajax(url:"PageIJustCreated.asp?id1=test&id2=test. success: etc. etc.) Yes you're using AJAX on a page on your same domain, which is why it works. The page you're calling is executing code to an outside web service server-side, so you end with no cross-domain problems. – user1290284 May 23 '12 at 00:14

1 Answers1

2

This SO Question should help you call the service server side: Calling REST web services from a classic asp page

To Summarise, use MSXML2.ServerXMLHTTP

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.open "GET", "Rest_URI", False
HttpReq.send

And check out these articles:

You will also need a way to parse JSON

Community
  • 1
  • 1
Jon P
  • 19,442
  • 8
  • 49
  • 72
  • Hi Jon, thank you for providing those resources. I didn't need to do anything more than including CDN jQuery to parse my responses. Will that not work here? – user1290284 May 18 '12 at 15:07
  • If I'm not mistaken, isn't server side script run when the page is loaded? I can't validate user input when the page is loaded, I have to wait until they explicitly click a button. – user1290284 May 18 '12 at 17:48
  • Yes the server side script runs on page load. But it can also run in response to a form being posted, i.e a button being clicked in a form tag. Check out this article on the request object on how you can access user input http://www.codeguru.com/csharp/.net/net_asp/article.php/c19325/An-Overview-of-Classic-ASPs-Request-Object.htm – Jon P May 19 '12 at 00:53