How should an array be passed to a Javascript function from C#?

后端 未结 4 959
误落风尘
误落风尘 2021-02-02 17:59

I use a WebBrowser object from WPF and I\'m calling some Javascript code in the page loaded in the browser like this:

myWebBrowser.InvokeScript(\"myJsFunc\", new         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 19:01

    Maybe pass it as a json string instead and parse it in the js function

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var json = serializer.Serialize(bar.ToArray());
    
    myWebBrowser.InvokeScript("myJsFunc", new object[] { foo.Text, json });
    

    js:

    function myJsFunc(json) {
       var data = JSON.parse(json);
       // do something with it.
    }
    

    http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx

提交回复
热议问题