2

I am calling Execute method of javascript function from C# using V8ScriptEngine but i got error ReferenceError: XMLHttpRequest is not defined.I installed nuget packages like Xhook but not able to resolve.Please help me to reolve this or suggest way to call api from javascript function and call function from c# using ClearScript(v8ScriptEnigne).

Javascript function:

 function Execute(paraList)
    {
    var finalUrl = "http://172.29.134.69:9006/api/Line/GetStationDefectsByStationId?stationId=1";
    var sol=get(finalUrl,function(){var resp=this.response;return resp;});
    return sol;  
    }
    function get(url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open("GET",url, true);
        xhr.send("");
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {          
                if (typeof callback === "function") {             
                    callback.apply(xhr);
                }
            }
        };



}

C# code:calling javascript function from c# using V8ScriptEngine

V8ScriptEngine _v8Engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);        
    _v8Engine.Execute(Script_Text);       
    _v8Engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
    object returnedVal = _v8Engine.Script.Execute();      
    return returnedVal;

1 Answers1

2

XMLHttpRequest is one of the Web APIs provided by browsers, whereas ClearScript and V8 provide a pure JavaScript environment.

If you just need a way to call HTTP services, you can expose something like System.Net.WebClient.

If you actually need XMLHttpRequest, you have a couple of options. You can expose the Windows COM implementation:

_v8Engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");

Or you can implement it in C# or another .NET language and expose that. One such implementation is here.

EDIT: Working sample using a synchronous request:

engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
engine.Execute(@"
    function get(url) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, false);
        xhr.send();
        if (xhr.status == 200)
            return xhr.responseText;
        throw new Error('Request failed: ' + xhr.status);
    }
");

Console.WriteLine(engine.Script.get("https://www.google.com"));
BitCortex
  • 3,328
  • 1
  • 15
  • 19
  • I tried with _v8Engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP") but i got this error (Error: Property get method is unavailable or inaccessible) in the open method in javascript function.Is the javascript function which i wrote is correct? – ramesh santha Dec 20 '18 at 06:03
  • @rameshsantha Update your question and post the exact code you're running. Your current JS snippet has syntax errors. – BitCortex Dec 20 '18 at 14:44
  • Hmm, I can't reproduce your issue with ClearScript 5.5.4. Beyond that, your code seems to be using an asynchronous HTTP request incorrectly. Your `get` and `Execute` functions always return `undefined`. – BitCortex Dec 21 '18 at 17:48
  • Could you correct the JavaScript functions to call Http request asynchronously ?My function always returned undefined value. – ramesh santha Dec 22 '18 at 14:49
  • You can't use asynchronous requests with a simple `result = SendRequest(...)` calling pattern, especially when relying on the COM `XMLHttpRequest`, which has specific threading requirements. I'll update my answer with a working sample using synchronous requests. – BitCortex Jan 02 '19 at 18:03