Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用jquery刷新验证码图像_C#_Javascript_Jquery_Asp.net Mvc_Captcha - Fatal编程技术网

C# 使用jquery刷新验证码图像

C# 使用jquery刷新验证码图像,c#,javascript,jquery,asp.net-mvc,captcha,C#,Javascript,Jquery,Asp.net Mvc,Captcha,我在剃须刀页面上有一个HTML图像标签。此图显示生成的验证码。我添加了一些jquery以在单击此图像时刷新它。这在我的电脑上有效,但一旦我将其发布到服务器上,单击图像时不会发生任何事情 <img id="captcha-image" src="@Url.Action("Captcha", "Home")" /> ... $("#captcha-image").click(function () { this.src = "@Url.Action("Captcha", "Hom

我在剃须刀页面上有一个
HTML
图像标签。此图显示生成的验证码。我添加了一些jquery以在单击此图像时刷新它。这在我的电脑上有效,但一旦我将其发布到服务器上,单击图像时不会发生任何事情

<img id="captcha-image" src="@Url.Action("Captcha", "Home")" />
...
$("#captcha-image").click(function () {
    this.src = "@Url.Action("Captcha", "Home")";
});
这将不起作用,因为
Next()
System.Random
中不是静态方法,我需要引用一个
Random
对象才能使用它

我还尝试使用JavaScript随机方法:

$("#captcha-image").click(function () {
    var randomNumber = Math.floor(Math.random());
    this.src = "@Url.Action("Captcha", "Home", new { r = randomNumber})";
});

这里的问题是变量
randomNumber
@Url中的c#code块范围内不可见。Action

您可以使用replace更改randomNumber字符串及其值,也可以将外部双引号更改为single,并将randomNumber传递给匿名对象初始值设定项

this.src = '@Url.Action("Captcha", "Home", new { randomNumber = "_randomNumber_"})'.replace("_randomNumber_", randomNumber );
另一种方法是将服务器端代码与javascript连接起来,生成查询字符串

this.src = '@Url.Action("Captcha", "Home")?randomNumber' + randomNumber ;

this.src='@Url.Action(“验证码”,“主页”)?randomNumber='+randomNumber;为什么不在操作方法上使用
[OutputCache(NoStore=true,Duration=0,VaryByParam=“*””)
来防止缓存
this.src = '@Url.Action("Captcha", "Home")?randomNumber' + randomNumber ;
//Put This one Partial view

<img src="@Url.Action("GetCaptchaImage", "Vulpith_Public",new { dt=@DateTime.Now},Request.Url.Scheme)" />
     //And call ajax from this partial view as following

function Captcharefresh() {
$.ajax({
             url: "../Vulpith_Public/_GetCaptcha",
             type: "post",
             dataType: "html",
             data: {},
success: function (data) {
    $('#codeRefresh').html(data)
},
error: function (data) {
}
});
}


//and create actions as following

public FileResult GetCaptchaImage(DateTime dt)
{
    CaptchaRandomImage CI = new CaptchaRandomImage();
    this.Session["CaptchaImageText"] = CI.GetRandomString(5); // here 5 means I want to get 5 char long captcha
    //CI.GenerateImage(this.Session["CaptchaImageText"].ToString(), 300, 75);
    // Or We can use another one for get custom color Captcha Image 
    CI.GenerateImage(this.Session["CaptchaImageText"].ToString(), 300, 75, Color.DarkGray, Color.White);
    MemoryStream stream = new MemoryStream();
    CI.Image.Save(stream, ImageFormat.Png);
    stream.Seek(0, SeekOrigin.Begin);
    return new FileStreamResult(stream, "image/png");
}


//One more action as
[HttpPost]
public ActionResult _GetCaptcha()
{
    return PartialView();
}