2

I'm trying to invoke javascript function in webbrowser. Website have separate file with javascript functions. This is a part of website html file:

<div class="header">
    <a class="buttonrg" onclick="$(this).hide();remove('56442741')"> Remove </a>
</div>

This is remove function from .js file:

function remove(id) {
    $.ajax({
        type: "POST",
        url: "ajax/remove.php",
        data: "remove=" + id
    });
}

And I'm trying to call 'remove' function with this script in c#:

    public void RemoveOffer(int _id)
    {
        try
        {
            webBrowser.Document.InvokeScript("remove", new object[] { _id.ToString() });
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
     }

but always when I'm trying to call this script console is showing me an error: specified cast is not valid.

What could went wrong?

KingZero
  • 21
  • 2

2 Answers2

0

Change data: "remove=" + remove to data: { "remove": id }

cyber_rookie
  • 665
  • 5
  • 9
  • Yeah, script on website is working, but when I'm trying to invoke this in webbrowser form it's still showing me the same message, so problem is in c# script. – KingZero Apr 15 '15 at 13:36
  • Why don't you remove the ToString() call? It's an object array so the int would work as well. – cyber_rookie Apr 15 '15 at 13:40
  • I was trying to call it with String array before, that's why there is ToString(). Just forgot to remove it, but it's not working w/o this as well. – KingZero Apr 15 '15 at 13:45
  • yeah.. still the same – KingZero Apr 15 '15 at 14:42
0

Are you calling the InvokeScript() from a thread other than the one that created it? Doing this caused this exception to be thrown for me.

It's a strange error message given the circumstances, however I resolved this with an Invoke()

e.g.

    private void RemoveOffer(int _id)
    {
        if (webBrowser.InvokeRequired)
        {
            webBrowser.Invoke(new Action(() => { RemoveOffer(_id); }));
            return;
        }
        webBrowser.Document.InvokeScript("remove", new object[] { _id }); // Not sure if the .ToString() is required...
    }
komodosp
  • 3,316
  • 2
  • 30
  • 59