简体   繁体   English

Ajax post 总是会导致错误,即使一切看起来都是正确的

[英]Ajax post always results in error, even though everything seems correct

I'm trying to process captcha in form validation using google's reCaptcha.我正在尝试使用谷歌的 reCaptcha 在表单验证中处理验证码。 Basically, my validation function is called using onSubmit in the form tag, and then calls a second function to work with the recaptcha api.基本上,我的验证函数是在表单标签中使用 onSubmit 调用的,然后调用第二个函数来使用 recaptcha api。

Here's the code:这是代码:

        var returnValue;

        var myData = {
            privatekey  : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            remoteip : ip,
            challenge : challenge,
            response : response
        };

        $.ajax({
            url: "http://www.google.com/recaptcha/api/verify",
            type: "POST",
            data: JSON.stringify(myData),
            dataType: "text",
            success: function(data) {
                var result = data.split("\n");
                if (result[0] == "true") {
                    returnValue = true;
                }
                else {
                    returnValue = false;
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert("There was an error submitting the captcha.  Please contact an administrator. \n\nError:\n" + textStatus, errorThrown);
                returnValue = false;                    
            },
            complete: function(jqXHR, textStatus) {
                return returnValue;
            }
        });

Using LiveHTTPHeaders in firefox, I can see the request go out to the service, and it looks like everything is being sent correctly.在 firefox 中使用 LiveHTTPHeaders,我可以看到请求发送到服务,看起来一切都被正确发送。 I get an HTTP/1.1 200 OK response back, and yet every single time the code goes to the error function.我得到一个 HTTP/1.1 200 OK 响应,但每次代码都转到错误函数。 When the error function runs, jqXHR is:当error函数运行时,jqXHR为:

Object { readyState=0, status=0, statusText="error"}对象 { readyState=0, status=0, statusText="error"}

textStatus is "error", and errorThrown is "" textStatus 是“error”,errorThrown 是“”

I've tried doing this a number of ways, including $.POST, and using .done(), .fail(), .always(), and it always behaves the same.我尝试了多种方法,包括 $.POST,并使用 .done()、.fail()、.always(),并且它的行为始终相同。

I've seen some other postings here having to do with problems with cross-domain requests, but none of those situations really seem to be relevant, because I actually am doing a cross-domain request, and those seemed to be issues where they were making requests to a file on the same domain, and it was being handled incorrectly as a cross-domain request.我在这里看到了一些其他与跨域请求问题有关的帖子,但这些情况似乎都没有真正相关,因为我实际上正在执行跨域请求,而那些似乎是他们所在的问题向同一个域上的文件发出请求,但它被错误地作为跨域请求处理。

I'm at my wits end here.. any help would be greatly appreciated.我在这里不知所措..任何帮助将不胜感激。

I've seen some other postings here having to do with problems with cross-domain requests, but none of those situations really seem to be relevant, because I actually am doing a cross-domain request我在这里看到了一些其他与跨域请求问题有关的帖子,但这些情况似乎都不相关,因为我实际上是在执行跨域请求

When I run that code I get the error:当我运行该代码时,出现错误:

XMLHttpRequest cannot load http://www.google.com/recaptcha/api/verify.
Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

So it is a cross-domain issue.所以这是一个跨域的问题。 You might want to make a cross domain request, but Google isn't set up to allow it.您可能想要发出跨域请求,但 Google 未设置为允许它。

I'm pretty sure that recaptcha needs to be implemented with server side code.我很确定 recaptcha 需要用服务器端代码来实现。 If you do it entirely on the client with JS then the client can lie and say it has passed the captcha when it hasn't.如果您使用 JS 完全在客户端上执行此操作,那么客户端可以撒谎并说它已经通过了验证码,而实际上还没有。

function check(){
    $.post('check.php',{
        'check':1,
        'challenge':$('#recaptcha_challenge_field').val(),
        'response':$('#recaptcha_response_field').val()},
        function(a){
            console.log(a);
        });
}

check.php:检查.php:

if($_POST){
    if(isset($_POST['check'])){
        $url = 'http://www.google.com/recaptcha/api/verify';

        $data = array(
            'privatekey'=>  "**********************************",
            'remoteip'  =>  $_SERVER['REMOTE_ADDR'],
            'challenge' =>  $_POST['challenge'],
            'response'  =>  $_POST['response'],
        );
        $options = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($data),           
            ),
        );
        echo print_r($data,true);
        die(file_get_contents($url, false, stream_context_create($options)));
    }
}

Warning: You have only one choice to check!警告:您只有一种选择来检查! ( Ref. ) (参考)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM