title: Error parsing script headers in admin-ajax with an HTTP request
body: <p>I am working on Wordpress and I have a strange error. This is my jQuery code</p>
  
  <pre><code>add_to_my_cart: function () {
              $(document).on('submit', 'form.form-submit', function (event) {
                  event.preventDefault();              
                  var data = $(this).serializeArray();
  
                  $.ajax({
                      url     : wp_custom_settings.ajax,
                      type    : 'POST',
                      data    : data,
                      dataType: 'html',
                      success : function (code) {
                          code = parseJSON(code);
                          window.location.href = code.redirect;
                      }
                  });
              });
          }
  </code></pre>
  
  <p>The above is called when I submit a form and pass some data to the PHP function. However, I get a <code>500 internal server error</code> on admin-ajax HTTP request, with the following on apache logs.</p>
  
  <pre><code>Premature end of script headers: admin-ajax.php, referer: my.domain.here
  AH01070: Error parsing script headers, referer: my.domain.here
  </code></pre>
  
  <p>I used the <code>error_log($message)</code> in every step of the process and I haven't found any problem. The very last step is the return of the JSON request.</p>
  
  <pre><code>if ( ! function_exists( 'my_send_json' ) ) {
  
  function my_send_json( $data ) {
      error_log("DONE");
      @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
      echo '&lt;!-- WP_AJAX_START --&gt;';
      echo wp_json_encode( $data );
      echo '&lt;!-- WP_AJAX_END --&gt;';
      if ( defined( 'DOING_AJAX' ) &amp;&amp; DOING_AJAX ) {
          wp_die();
      } else {
          die;
      }
  }
  }
  </code></pre>
  
  <p>I can see on the <code>debug.log</code> the string <code>DONE</code>, but the HTTP request has an empty response. I tried also to remove the <code>$data</code> and just return this</p>
  
  <pre><code>echo wp_json_encode( array( 'status' =&gt; 'success', 'redirect' =&gt; 'my.domain.here' ));
  </code></pre>
  
  <p>But still the same. I cannot think of any other way to debug it.</p>