使用 jQuery 验证 RECAPTCHA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/540706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:18:27  来源:igfitidea点击:

Verifying RECAPTCHA with jQuery

jqueryrecaptcha

提问by Magnar

I'm trying to verify a Recaptcha using jQuery, but I keep getting an error telling me: Access to restricted URI denied" code: "1012

我正在尝试使用 jQuery 验证 Recaptcha,但我不断收到错误消息,告诉我:访问受限 URI 被拒绝”代码:“1012

This is what I've tried so far:

这是我迄今为止尝试过的:

 var challengeVal = $("#recaptcha_challenge_field").attr("value");
 var reponseVal = $("#recaptcha_response_field").attr("value");
 var remoteIp = <%= "'" + Request.ServerVariables["REMOTE_HOST"] + "'" %>
 var privateKey = 'MY_PRIVATE_KEY';

 var requestUrl = "http://api-verify.recaptcha.net/verify?privatekey=" + privateKey + "&remoteip=" + remoteIp + "&challenge=" + challengeVal + "&response=" + reponseVal;

 $.ajax({
    type: "POST",
    url: requestUrl,
    dataType: "json",
    success: function(data) {
       alert('response from recaptcha');
    },
    error: function() {
       alert("An error occured.");
    }
  });

Anyone tried this, who can point me in the right direction?

任何人都试过这个,谁能指出我正确的方向?

Thanks.

谢谢。

回答by Magnar

JavaScript is prohibited from making cross-domain XMLHttpRequests for security reasons. There are workarounds, but they only work if you control both domains.

出于安全原因,禁止 JavaScript 进行跨域 XMLHttpRequest。有一些解决方法,但它们只有在您控制两个域时才有效。

Solution: Make an AJAX-call to your own server, and contact recaptcha through server side code.

解决方案:对自己的服务器进行AJAX调用,通过服务器端代码联系recaptcha。

回答by hanoo

i would look -> racaptcha docs AJAXthere is a full example in javascript.

我会看 -> racaptcha docs AJAX在 javascript 中有一个完整的例子。

回答by Guido Lemmens 2

A full demo can be found and downloaded from this page. But you still need to generate the public and private keys for your domain here https://www.google.com/recaptcha/admin/create

可以从此页面找到并下载完整的演示。但是您仍然需要在此处为​​您的域生成公钥和私钥https://www.google.com/recaptcha/admin/create

回答by mmengel

@Magnar already answered w/ respect to the security reasons. @Guido Lemmens 2 gave a PHP example. I wanted to add some ASP.NET WebForms (vs. MVC) code from another Stack question.

@Magnar 已经就安全原因做出了回答。@Guido Lemmens 2 给出了一个 PHP 示例。我想从另一个 Stack question添加一些 ASP.NET WebForms (vs. MVC) 代码。