1

I am trying to execute javascript function from c# code using ClearScript(V8ScriptEngine). Here Script_Text is javascript code. I want to call api from jquery and load Execute function in C#.I got exception like $ is not defined.How can i load jquery in C# using clearscript(V8ScriptEngine)?

C# code:

public dynamic GetText(string Script_Text, List<object> paraList)
{
  V8ScriptEngine _v8Engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);     
 _v8Engine.Execute(Script_Text);
  object returnedVal = _v8Engine.Script.Execute();      
  return returnedVal;
}

Javascript Function:

function Execute(){
    $.ajax({
      type: 'GET',                
      url: "http://172.29.134.69:9006/api/Line/GetStationDefectsByStationIdstationId=1",
      success: function (data) {
      alert(data);
      },
      error: function (error) {
        alert("Fail");
        }
      });

1 Answers1

0

You could download and try to execute jQuery before running your script, but that would probably fail because the bare JavaScript environment lacks the DOM that jQuery depends on.

You could provide a DOM implementation to underpin jQuery, but I suspect that would be a large effort, although there are public projects such as jsdom that could help.

If ajax and alert are all you need, it would be much easier to implement them in C# or another .NET language and expose them for scripting.

BitCortex
  • 3,328
  • 1
  • 15
  • 19
  • It is not to use ajax or alert.I will upload javascript function in .txt file and execute it in C# dyamically.So please suggest me the way to write javascript function to call api and also how to load in C# and execute the function . – ramesh santha May 15 '19 at 10:55
  • You can expose C# objects, types, delegates, etc. and use them from JavaScript; there's a ClearScript tutorial on GitHub. You just can't use jQuery easily because jQuery itself requires the DOM, which is usually provided by web browsers but isn't part of standard JavaScript. – BitCortex May 15 '19 at 14:02