JSON stringify returns null (C # WebBrowser control)

I am trying to get a string representation of my JSON object back to my C # application, but every time I use JSON.stringify(jsonObj) , it returns null in my application. I am using a WebBrowser .

The page is configured as follows:

 var myObj = { "foo": [] } // Push data into it function getMyObj() { return JSON.stringify(myObj); } 

In my C # code, I have this:

  string bar = myWebBrowser.MainBrowser.Document.InvokeScript("getMyObj").ToString(); 

However, after calling the method, it returns null . I even tried setting alert on a JavaScript page where I call the getMyObj() function inside alert , but the warning never appears.

Is this a webbrowser management issue? JSON must be supported, the browser uses IE11.

0
source share
1 answer

I used the meta tag to get around this. Apparently, WebBrowser Control defaults to IE7. There are registry settings that you can change to fix this, but I did not want to change the user registry settings.

This question helped me find a solution: Use the latest version of Internet Explorer in a web browser control

 <html> <head> <meta http-equiv='X-UA-Compatible' content='IE=edge' > ... </head> <body> ... <script> var myObj = { "foo": [] } // Push data into it function getMyObj() { return JSON.stringify(myObj); } </script> </body> </html> 

Hope this helps!

0
source

Source: https://habr.com/ru/post/1274987/


All Articles