0

我正在尝试在 Content Grabber 中使用 C# 调用 Javascript 函数。(Content Grabber 是一个网页抓取软件)。

Javascript代码是这样的:

$.definePage({
    idRecaptcha: null,

    init: function() {},

    carregarReCaptcha: function() {
        if(page.idRecaptcha == null) {
            var sitekey = $("#reCaptchaPublicKey").val();
            page.idRecaptcha = grecaptcha.render($("#tecRecaptcha")[0], {
                'callback' :  page.verifyCallback,
                'sitekey': sitekey
            });
        }
    },

    verifyCallback: function(response) {
        if(response) {
            $("#form").submit();
        }
    }
});

var onloadCallback = function() {
    page.carregarReCaptcha();
}

我要调用的函数是“verifyCallback”。这个函数本质上是提交recaptcha token,它将验证我输入的token是否正确。

在我的 Content Grabber 代理中,我想调用这个函数,我有这个代码,但它给了我一个错误:

using System;
using System.Web.UI;
using Sequentum.ContentGrabber.Api;
public class Script
{
    //See help for a definition of CustomScriptArguments.
    public static CustomScriptReturn CustomScript(CustomScriptArguments args)
    {
        // retrieve page from current handler
        var page = System.Web.HttpContext.Current.CurrentHandler as Page;

        if (page == null)
        {
            // do something, e.g. throw exception
            return CustomScriptReturn.Pause();
        }

        // Place your script code here.
        // Return empty for no special action.
        string response = args.DataRow["Captcha"];
        string script = "page.verifyCallback('" + response + "');";

        // call ClientScript from existing page instance
        page.ClientScript.RegisterStartupScript(page.GetType(), "page.verifyCallback", script, true);

        return CustomScriptReturn.Empty();
    }
}

当我编译它时,它返回此错误:

Object reference not set to an instance of an object.

看来我不能只删除object sender, EventArgs e

我对 JS 或 C# 不是很熟悉,所以如果能得到任何帮助,我将不胜感激。太感谢了!

4

2 回答 2

1

出现问题是因为您尝试使用ClientScript非继承自类的实例System.Web.UI.Page(代码隐藏页面的基类)。只要您有权访问,就可以从其处理程序属性(即)HttpContext.Current中检索实例并使用如下示例:PageCurrentHandlerClientScript

public class Script
{
    //See help for a definition of CustomScriptArguments.
    public CustomScriptReturn CustomScript(CustomScriptArguments args, object sender, EventArgs e)
    {
        // retrieve page from current handler
        var page = HttpContext.Current.CurrentHandler as Page;

        if (page == null)
        {
            // do something, e.g. throw exception
        }

        // Place your script code here.
        // Return empty for no special action.
        string response = args.DataRow["Token"];
        string script = "verifyCallback('" + response + "');";

        // call ClientScript from existing page instance
        page.ClientScript.RegisterStartupScript(page.GetType(), "verifyCallback", script, true);
        return CustomScriptReturn.Empty();
    }
}

更新:

至于解释编辑后的第二个错误,这是因为您声明了一个名为callbackinside CustomScriptmethod 的方法,该方法无效(并且该return语句必须在最后)。如果senderandEventArgs处理程序是不必要的,那么简单地省略它们。这是一个正确返回的示例CustomScriptReturn

public static CustomScriptReturn CustomScript(CustomScriptArguments args)
{
    // retrieve page from current handler
    var page = System.Web.HttpContext.Current.CurrentHandler as Page;

    if (page == null)
    {
        // do something, e.g. throw exception
    }

    // Place your script code here.
    // Return empty for no special action.
    string response = args.DataRow["Captcha"];
    string script = "page.verifyCallback('" + response + "');";

    // call ClientScript from existing page instance
    page.ClientScript.RegisterStartupScript(page.GetType(), "page.verifyCallback", script, true);

    return CustomScriptReturn.Empty();
}

相关问题:

非静态字段、方法或属性“System.Web.UI.Page.ClientScript.get”需要对象引用

于 2018-11-06T05:37:56.903 回答
1

除了@Tetsuya 的回答之外,您传递给RegisterStartupScript 的Javascript 似乎还有问题。

我认为您不能只调用“verifyCallback”,因为这是在$.definePage({})调用范围内定义的函数。

因此,您需要更深入地研究并确定该verifyCallback功能是否公开。

基于此代码:

var onloadCallback = function() {
    page.carregarReCaptcha();
}

我会说你需要打电话:

page.ClientScript.RegisterStartupScript(page.GetType(), "page.verifyCallback", script, true);
于 2018-11-06T05:50:26.263 回答