使用 C# 的脚本注入 returns null

Script Injection returns null using C#

我正在尝试向网页添加 ajax 函数并使用 C# 执行它。这是我的代码:

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { $.ajax({type: 'GET', url: '/gres/ajax/archive-odds.php',data:'&outcome='+'27498xv498x0x0'+'&provider='+'14',cache:!1,dataType:'xml', success: function (data) { alert('sucess'); }, complete: function () {} }); $('body').html(); }";
head.AppendChild(scriptEl);
object res = webBrowser1.Document.InvokeScript("sayHello");

我的代码没有给我任何错误。但是 res 对象始终为空。有人可以帮帮我吗??

谢谢

您的 js 函数 return 没有任何值。

试试 this example