2012-10-08 27 views
0

我正在嘗試使用google的reCaptcha在表單驗證中處理驗證碼。基本上,我使用form標籤中的onSubmit調用了我的驗證函數,然後調用第二個函數與recaptcha api一起工作。即使一切似乎都正確,Ajax帖子總是會導致錯誤

下面的代碼:

 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響應,而且每次代碼都進入錯誤函數。當誤差函數運行,jqXHR是:

對象{readyState的= 0,狀態= 0,狀態文本= 「錯誤」}

textStatus是 「錯誤」,並errorThrown是 「」

I」我嘗試了很多方法,包括$ .POST,並使用.done(),.fail(),.always(),它總是表現相同。

我見過其他一些帖子,這裏有與跨域請求有關的問題,但這些情況都沒有真正相關,因爲我實際上正在做一個跨域請求,而那些似乎是他們向同一個域上的文件發出請求的問題,並且作爲跨域請求被錯誤地處理。

我在我的智慧結束在這裏..任何幫助將不勝感激。

+4

你可能不應該在SO問題中發佈你的私鑰。 – jbabey

+0

一個側面說明:'我嘗試了很多方法,包括$ .POST,並使用.done(),.fail(),.always(),它總是表現相同。「那些3種方法的用途與錯誤,成功和完整相同。根據文檔,現在應該在功能上有所不同,除非他們現在更願意準備成功,錯誤和完成將來將被棄用。 – Nope

回答

2

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

當我運行代碼,我得到的錯誤:

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完全在客戶端上完成,那麼客戶端可以說謊,並說它沒有通過驗證碼。

+0

感謝您的信息。我從來沒有看到這個錯誤,但是我最終實現了一種不同類型的驗證碼。我很喜歡使用服務器端的東西,但我想添加的這個網站只是純HTML,我不想重新設計整個事情:/(加上客戶端不想支付那麼多,大聲笑)。 – Redit0

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

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

警告:你只有一個選擇,檢查! (Ref.

相關問題