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.

4
  • Any errors in the console? What if you console.log(myObj) just before stringifying it? Commented Mar 14, 2017 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. Commented Mar 14, 2017 at 16:22
  • yeah but what does the data look like? Is it valid? Is it "stringifyable"? Commented Mar 14, 2017 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. Commented Mar 14, 2017 at 16:42

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.