Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is WP7 WebBrowser InvokeScript call throwing Error: 80020101?

This error seems to throw for anything that goes wrong in the javascript function that is called:

SystemException was unhandled    
An unknown error has occurred. Error: 80020101.

I am using the javascript as an interface to a web-app backend, and I have two calls that are working fine, loading and returning JSON from an ajax call, that look like this:

In Silverlight:

MyBrowser.InvokeScript("getData", "/Me/Feed?numberOfResults=-1", "MyFeed");

The Javascript loaded into the WebBrowser

function getData(url, context) {
    $.ajax({
        url: url,
        dataType: 'jsonp',
        success: function (result) {
            callback(result, context);
        }
    });
}

But later I want to post data back to the server, and I am doing the same thing:

MyBrowser.InvokeScript("postData", "thedata", "CreatePost");

function postData(payload, context) {
    $.ajax({
        type: "POST",
        url: "/Post/Create?" + tz(),
        data: payload,
        dataType:"json",
        success:function (result) {
            callback(result, context);
        }
    });
}

Now I get the exception.

What is really strange is that I can immediately call the function from within the script and it posts to the back-end just fine.

postData("sampledata", "PostTest");

At first it seemed the only difference was that one was a GET and the other a POST, so I copied the GET ajax call into the second call (which happens on user input, where the first one happened on load). The result was the same (I get the same error). I can get other commands to fire in the javascript from this event as long as they don't contain ajax calls (it appears). So you might think that it is a timing thing - so I moved the call up above where I call the other InvokeScripts which are working, and it still doesn't work (same exception).

I also tried calling it on a separate thread, using Dispatcher.BeginInvoke for good measure and no dice.

Dispatcher.BeginInvoke(() =>
{
    MyBrowser.InvokeScript("postData", "thedata", "CreatePost");
});

I'm completely baffled. There does seem to be some consistency, in that if a call fails it will fail every time, but I can't tell what is the difference between the calls that work and the ones that fail.

Can anyone tell me what I am doing wrong, or what it is I don't understand about using InvokeScript and Ajax together??

Thanks!

[EDIT - adding this inline (was in the comments) because I'm getting the question a lot]

I have worked on this for 6 hours and this is what I'm seeing:

  • there are 2 different events from which I am making these calls; 1) when the page load has completed on the browser control, 2) when a user taps my "post" button
  • the error does NOT occur when the Ajax call is a GET from the load event
  • the error DOES occur when calling the same GET call from the user event
  • the error ALSO DOES OCCUR when calling the Ajax call using POST from the load event
  • the error does NOT occur when calling a function that does not attempt any Ajax from the user event
like image 791
Subcreation Avatar asked Mar 25 '11 03:03

Subcreation


2 Answers

You'll get the 80020101 error if the javascript method can't be found or th JS throws an error.

In terms of finding the method, the control will match the signature exactly. This is not the standard javascript behaviour so be careful with this.

Beware of caching of the page (and it's contents) within the browser control as this catches a lot of people out. :(

like image 133
Matt Lacey Avatar answered Oct 22 '22 18:10

Matt Lacey


Seeing the GET working with JSONP, and the POST not working at all, seems to imply that you may be going cross-domain? Typically on the same domain there is no need to use JSON*P*.

So my guess is that it is a violation of the same-origin policy.

like image 42
Dan Marshall Avatar answered Oct 22 '22 17:10

Dan Marshall