Search code examples
c#jqueryjsonbrowserinvokescript

InvokeScript, JavaScript Error


I have an IE9 Web browser control embedded in a larger WPF application. I'm attempting to call InvokeScript and pass it a JSON string as a parameter:

webBrowser.InvokeScript("redrawPlot", new object[] { reDrawData });

The function redrawPlot uses the jquery method parseJSON to parse this back into an object:

redrawPlot = function(dataObj) {
        dataObj = $.parseJSON(dataObj);
        ...
}

When this runs in the WPF app I get the JavaScript error:

Invalid character

The contents of reDrawData (inspected in script debugger and in the WPF app) are:

"{\"plot0\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot1\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot2\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot3\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot4\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}]}"

When I run this in under IE9 developer tools passing that string directly I get no errors.

Am I not calling InvokeScript correctly?


Solution

  • The quotes around you properties and strings are escaped, they should not be. That is

    {\"plot0\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot1\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot2\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot3\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}],\"plot4\":[{\"data\":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],\"label\":\"A-TOP-6\"}]}

    should be

    {"plot0":[{"data":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],"label":"A-TOP-6"}],"plot1":[{"data":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],"label":"A-TOP-6"}],"plot2":[{"data":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],"label":"A-TOP-6"}],"plot3":[{"data":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],"label":"A-TOP-6"}],"plot4":[{"data":[[1,1111.11111111111],[2,1111.11111111111],[3,1111.11111111111]],"label":"A-TOP-6"}]}