1

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 to my application. I am using a WebBrowser control.

Page is set up like this:

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 invoking the method, it returns null. I even tried putting an alert in the JavaScript page where I call the getMyObj() function inside the alert, but the alert never comes up.

Is it a WebBrowser control issue? JSON should be supported, the browser is using IE11.

cress
  • 389
  • 3
  • 13
  • Any errors in the console? What if you `console.log(myObj)` just before stringifying it? – Jeremy Thille Mar 14 '17 at 16:14
  • If I test it in a web browser such as IE, Firefox, or Chrome, it prints out the data inside the object. In the application, `console.log(myObj)` doesn't appear in the console so I get no output. – cress Mar 14 '17 at 16:22
  • yeah but what does the data look like? Is it valid? Is it "stringifyable"? – Jeremy Thille Mar 14 '17 at 16:29
  • I see an object with an array of data. It looks valid. When I stringify it in IE, Firefox, or Chrome: `{"data": [{"field1": "value"},{"field1": "value"}]}`. But nothing in the C# application. – cress Mar 14 '17 at 16:42

1 Answers1

1

I used the meta tag to get around this. Apparently WebBrowser Control defaults to IE7. There are registry settings you can change to correct this but I did not want to modify users registry settings.

This question helped me get to the solution: Use latest version of Internet Explorer in the webbrowser 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!