0

I'm new to stack overflow and fairly new to programming so bear with me. I apologize in advance for any typos.

Planning to use C# console application to extract data from a Web API. Did not find alot of information on how to execute a javascript function from the .cs file. So i decided to try out Microsoft Script Control. I am trying to send a xmlhttpRequest using Microsoft Script Control. But I keep getting

"XMLHttpRequest is undefined".

        ScriptControl js = new ScriptControl();
        js.AllowUI = false;
        js.Language = "JScript";
        js.Reset();
        js.AddCode(@"
   function test()
    {

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('GET', 'WEB API LINK', true);
    xmlhttp.send();

    xmlhttp.addEventListener('readystatechange', processRequest, false);
    xmlhttp.onreadystatechange = processRequest;

   function processRequest(e) 
       {
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                var response = JSON.parse(xmlhttp.responseText);
                return response;
            }
        }
     }
  }
");

Not sure if this even works with Script Control. When using the script in a regular html page inside <script> </script> it works except that I havent been able to send data or retrieve data from .html to .cs. Any different routes would be appreciated.

Regards RiceNor

M. Adeel Khalid
  • 1,786
  • 2
  • 21
  • 24
RiceNor
  • 11
  • 5
  • Wait what no please don't! - download [like this](http://stackoverflow.com/questions/5566942/how-to-get-a-json-string-from-url) then parse json [like this](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Alex K. Mar 07 '17 at 12:24
  • Thank you, il check out those. – RiceNor Mar 07 '17 at 12:38
  • Any possibility to flag this as answered when it is only a comment? – RiceNor Mar 07 '17 at 12:39

1 Answers1

0

To get this answered i paste Alex K. comment Download like this How to get a json string from url?

then parse json like this How can I parse JSON with C#?

RiceNor
  • 11
  • 5