Verifying RECAPTCHA with jQuery

℡╲_俬逩灬. 提交于 2019-12-08 18:37:07

问题


I'm trying to verify a Recaptcha using jQuery, but I keep getting an error telling me: Access to restricted URI denied" code: "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.


回答1:


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

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




回答2:


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




回答3:


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




回答4:


@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.



来源:https://stackoverflow.com/questions/540706/verifying-recaptcha-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!