title: Getting null response to Recaptcha V3 with ajax
body: <p>I'm having a problem with getting a problem when submitting captcha v3 to ajax. I get a null response to Google. I have tried multiple tutorials but I think it does not fix to ajax request.</p>
  
  <p>I think the problem is with google or my php, or ajax request. I understand my codes but I am not familiar with using google's code like this captcha v3. I have use captcha v2 before. And now I am trying v3 I can't run it with my code. </p>
  
  <p>Here is my Ajax and HTML code :</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// when form is submit
      $('.contact-form').submit(function (event) {
          // we stoped it
          event.preventDefault();
          var url = $(this).prop('action');
          var contact_data = $(this).serialize();
          $('.btn-contact').html('&lt;em class="fas fa-spinner fa-pulse"&gt;&lt;/em&gt; Sending...');
          $('.alert').fadeOut('slow');
          // needs for recaptacha ready
          grecaptcha.ready(function () {
              // do request for recaptcha token
              // response is promise with passed token
              grecaptcha.execute('6Ldjp6Q[--my site key---]8H5bxI1', { action: 'create_contact' }).then(function (token) {
                  // add token to form
                  $('.contact-form').prepend('&lt;input type="hidden" name="g-recaptcha-response" value="' + token + '"&gt;');
                  var contact = function () {
                      $.ajax({
                          type: 'POST',
                          url: url,
                          dataType: 'json',
                          data: contact_data, token: token ,
                          success: function (response) {
                              $('.message').html(response.message);
                              $('.btn-contact').html('&lt;em class="fas fa-paper-plane"&gt;&lt;/em&gt; Send');
                              if (response.error) {
                                  $('.status').removeClass('alert-success').addClass('alert-danger').show();
                              }
                              else {
                                  $('.status').removeClass('alert-danger').addClass('alert-success').show();
                                  $('.contact-form')[0].reset();
                              }
                          }
                      });
                  };
                  setTimeout(contact, 3000);
              });;
          });
      });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?=form_open('Contact/Send_Contact', array('class' =&gt; 'contact-form'))?&gt;
              &lt;div class="row top-2"&gt;
                  &lt;div class="col-md-12"&gt;
                      &lt;div class="status alert text-center" style="display:none;"&gt;
                          &lt;button type="button" class="close clearMsg"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
                          &lt;span class="message"&gt;&lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;input type="text" placeholder="Your Name" name="name" class="form-control name"&gt;
                      &lt;div class="invalid-feedback invalid-name"&gt;&lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;input type="email" placeholder="Your Email" name="email" class="form-control email"&gt;
                      &lt;div class="invalid-feedback invalid-email"&gt;&lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group col-md-12"&gt;
                      &lt;input type="text" placeholder="Subject" name="subject" class="form-control subject"&gt;
                      &lt;div class="invalid-feedback invalid-subject"&gt;&lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group col-md-12"&gt;
                      &lt;textarea placeholder="Message" class="form-control message_text" name="message" rows="4"&gt;&lt;/textarea&gt;
                      &lt;div class="invalid-feedback invalid-message"&gt;&lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="col-md-12"&gt;
                      &lt;button type="submit" class="btn btn-info btn-contact float-right"&gt;&lt;em class="fas fa-paper-plane"&gt;&lt;/em&gt; Send&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;/from&gt;
  &lt;script src="https://www.google.com/recaptcha/api.js?render=6Ldj[--My site key--]5bxI1"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>And I also copied some PHP code and I don't know where is the problem here because this is my first time using CodeIgniter and I also haven't used captcha v3 too. 
  Please review my PHP code here :</p>
  
  <pre><code>public function Send_Contact() {
          $output['error'] = true;
          $output['message'] = 'Please fill up the error field(s).';
  
          $name = $this-&gt;input-&gt;post('name');
          $email = $this-&gt;input-&gt;post('email');
          $subject = $this-&gt;input-&gt;post('subject');
          $message = $this-&gt;input-&gt;post('message');
          $captcha = $this-&gt;input-&gt;post('token');
  
          $this-&gt;form_validation-&gt;set_rules('name', 'Name', 'required');
          $this-&gt;form_validation-&gt;set_rules('email', 'Email', 'required');
          $this-&gt;form_validation-&gt;set_rules('subject', 'Subject', 'required');
          $this-&gt;form_validation-&gt;set_rules('message', 'Message', 'required');
  
          if($this-&gt;form_validation-&gt;run() === false) {
              //some codes here I remove because it has nothing to do with the question.
              die(json_encode($output));
          }
  
          if(!$captcha){
              $output['message'] = "Invalid catpcha";
              die(json_encode($output));
          }
  
          $secretKey = "6Ldjp[--My secret key--]YQL_";
          $ip = $_SERVER['REMOTE_ADDR'];
  
          // post request to server
          $url = 'https://www.google.com/recaptcha/api/siteverify';
          $data = array('secret' =&gt; $secretKey, 'response' =&gt; $captcha);
  
          $options = array(
              'http' =&gt; array(
              'header'  =&gt; "Content-type: application/x-www-form-urlencoded\r\n",
              'method'  =&gt; 'POST',
              'content' =&gt; http_build_query($data)
              )
          );
  
          $context  = stream_context_create($options);
          $response = file_get_contents($url, false, $context);
          $responseKeys = json_decode($response,true);
  
          if($responseKeys["success"]) {
              if($this-&gt;contact_model-&gt;create_contact()) {
                  $output['error'] = false;
                  $output['message'] = 'Thank you for sending message! &lt;em class="far fa-check-circle"&gt;&lt;/em&gt;';
                  echo json_encode($output);
              } else {
                  $output['error'] = true;
                  $output['message'] = 'Error in database model.';
                  echo json_encode($output);
              }
          } else {
              $output['message'] = "Invalid catpcha";
              die(json_encode($output));
          }
      }
  </code></pre>
  
  <p>I'm using codeigniter 3. and this public function is in the contact controller.</p>