簡體   English   中英

Ajax post 總是會導致錯誤,即使一切看起來都是正確的

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

我正在嘗試使用谷歌的 reCaptcha 在表單驗證中處理驗證碼。 基本上,我的驗證函數是在表單標簽中使用 onSubmit 調用的,然后調用第二個函數來使用 recaptcha api。

這是代碼:

        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;
            }
        });

在 firefox 中使用 LiveHTTPHeaders,我可以看到請求發送到服務,看起來一切都被正確發送。 我得到一個 HTTP/1.1 200 OK 響應,但每次代碼都轉到錯誤函數。 當error函數運行時,jqXHR為:

對象 { readyState=0, status=0, statusText="error"}

textStatus 是“error”,errorThrown 是“”

我嘗試了多種方法,包括 $.POST,並使用 .done()、.fail()、.always(),並且它的行為始終相同。

我在這里看到了一些其他與跨域請求問題有關的帖子,但這些情況似乎都沒有真正相關,因為我實際上正在執行跨域請求,而那些似乎是他們所在的問題向同一個域上的文件發出請求,但它被錯誤地作為跨域請求處理。

我在這里不知所措..任何幫助將不勝感激。

我在這里看到了一些其他與跨域請求問題有關的帖子,但這些情況似乎都不相關,因為我實際上是在執行跨域請求

當我運行該代碼時,出現錯誤:

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

所以這是一個跨域的問題。 您可能想要發出跨域請求,但 Google 未設置為允許它。

我很確定 recaptcha 需要用服務器端代碼來實現。 如果您使用 JS 完全在客戶端上執行此操作,那么客戶端可以撒謊並說它已經通過了驗證碼,而實際上還沒有。

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

檢查.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)));
    }
}

警告:您只有一種選擇來檢查! (參考)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM