javascript - 如何在 WebBrowser 控件中捕获 POST 的结果?

标签 javascript c# winforms webbrowser-control

我有一个带有 WebBrowser control 的 Winforms 表单在上面。

我已经想出了如何通过将 C# 类的实例附加到 ObjectForScripting 来将 C# 代码连接到 Web 浏览器控件中的 Javascript。属性(property),像这样:

public partial class Browser : Form
{
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.ObjectForScripting = new ScriptInterface();
    }
}

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class ScriptInterface
{
    public void DoSomething(string data)
    {
        // Do something interesting with data here
    }
}

...然后像这样从 Javascript 中调用它:

<button onclick=window.external.DoSomething('with this')/>

我还没有弄清楚如何从 WebBrowser 控件中的表单捕获 POST 操作的结果,并在我的 C# 代码中使用它。

最佳答案

你或许可以使用 jQuery post而不是表单帖子。

假设您的表单的 ID 为 myForm:

$( "#myForm" ).submit(function( event ) {     
  // Stop form from submitting normally
  event.preventDefault();

  // Get some values from elements on the page:
  var $form = $(this),
  var term = $form.find("input[name='s']").val(),
  var url = $form.attr("action");

  // Send the data using post
  var posting = $.post( url, { s: term } )
      .done(function(data) {
          //Pass the response back to your code
          window.external.DoSomething(data);
      });
});

关于javascript - 如何在 WebBrowser 控件中捕获 POST 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281275/

相关文章:

javascript - 插入音频元素会导致 CSS 错误

javascript - 如何用 javascript 删除这种情况?

javascript - Flowplayer 实例的 $f 未定义

c# - 我什么时候应该使用 ClearALLPools 和 ClearPool?

c# - form.show 和 form.Activate 之间的区别

c# - .resx 表单图标级联更新

c# - 刷新 DataGridView

javascript - 表不显示用户数据

c# - 困难的层次排序

c# - 我们可以在 ServicePointManager.SecurityProtocol 中添加四个协议(protocol)吗?