title: Http 406 error on javascript ajax post. Accept header set to accept all
body: <p>I am getting an error from the server when I try to make an AJAX request that I cannot figure out. It is an HTTP 406 error. From what I understand this means that the server cannot send a response in the format that the Accept header indicates. But from what I can see, I am <code>Accept</code>ing <code>*/*</code> (wildcard). </p>
  
  <p>You can see the details of my HTTP request in this image.
   <a href="https://i.stack.imgur.com/fTfBf.png" rel="nofollow noreferrer">http://i.stack.imgur.com/fTfBf.png</a>
  <img src="https://i.stack.imgur.com/fTfBf.png" alt="My HTTP Request"></p>
  
  <p>This is my PHP code for the page:</p>
  
  <pre><code>&lt;body&gt;
  &lt;div id="container"&gt;
  
               (snip - menu and header)
  
      &lt;div id="notify-saved"&gt;
          &lt;div class="width"&gt;
              &lt;span&gt;Saved!&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div id="confirm-delete"&gt;
          &lt;div id="confirm-delete-content" class="width"&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div id="nav-separator"&gt;
      &lt;/div&gt;
      &lt;div id="body" class="width"&gt;
  
          &lt;section id="content"&gt;
              &lt;br /&gt;
      &lt;form method="POST" id='template-form' action="post-actions.php" &gt;
          &lt;input type='hidden' name='id' value="15" /&gt;
          &lt;input type='text' name='name' value="default-scripting" placeholder='Name' /&gt;
              &lt;br /&gt;
              &lt;br /&gt;
          &lt;input type='checkbox' name='major' id="majorminor" /&gt;
          &lt;textarea name='value' style="width:100%;"&gt;&amp;lt;script src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/textarea&gt;
              &lt;br /&gt;
              &lt;br /&gt;
  
          &lt;button name='action' value="Update Template" id='update-button' &gt;Update&lt;/button&gt;
  
  
  
      &lt;/form&gt;
              &lt;/section&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>// Attach a submit handler to the form
  $( "#update-button" ).click( function( event ) {
      // Stop form from submitting normally
      event.preventDefault();
  
      // Get some values from elements on the page:
      var $form = $('#template-form');
      var template_id = $form.find( "input[name='id']" ).val();
      var template_name = $form.find( "input[name='name']" ).val();
      var template_value = $form.find( "textarea[name='value']" ).val();
  
      var template_is_major = 'minor';
      if( $( "input[name='major']" ).prop('checked') ){
          template_is_major = "major";
      }
  
      var url = $form.attr( "action" );
  
      // Send the data using post
      var posting = $.post( url, {  id: template_id, name: template_name, value: template_value, major: template_is_major, action: "Update Template"  } );
  
      // Show that the request has completed successfully
      posting.done(function( data ) {
          $('#notify-saved').slideDown(200).delay(500).slideUp(500);
      });
  });
  </code></pre>
  
  <p>.</p>
  
  <p>Also of relevance is the fact that this error <em>only happens some of the time</em>, for instance if the template-value submitted is</p>
  
  <p><code>&amp;lt;script src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;</code></p>
  
  <p>then there will be an error, but if the leading <code>&lt;</code> is omitted, then there is no error: </p>
  
  <p><code>script src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;</code></p>
  
  <p>The receiving php looks like this (post-actions.php):</p>
  
  <pre><code>&lt;?php 
  
      // ugly-@$$ anti-magicquotes hackk
      if (get_magic_quotes_gpc()) {
          $process = array(&amp;$_GET, &amp;$_REQUEST, &amp;$_COOKIE, &amp;$_REQUEST);
          while (list($key, $val) = each($process)) {
              foreach ($val as $k =&gt; $v) {
                  unset($process[$key][$k]);
                  if (is_array($v)) {
                      $process[$key][stripslashes($k)] = $v;
                      $process[] = &amp;$process[$key][stripslashes($k)];
                  } else {
                      $process[$key][stripslashes($k)] = stripslashes($v);
                  }
              }
          }
          unset($process);
      }
  
  
      require_once('database.php');
      $dbo = new Database();
  
      if( isset( $_REQUEST['action'] ) ){
  
          switch( $_REQUEST['action'] ){
  
              (snip - many irrelevant cases)
  
              case "Update Template":
                  $dbo-&gt;UpdateTemplate( $_REQUEST[DB_TEMPLATES_ID], $_REQUEST[DB_TEMPLATES_NAME],
                  $_REQUEST[DB_TEMPLATES_VALUE], $_REQUEST[DB_TEMPLATES_MAJOR] );
                  break;
  
  
  
          }
  
      }
  </code></pre>
  
  <p>And the database class:</p>
  
  <pre><code>&lt;?php 
  
  require_once ("config.php");
  require_once ("database-contract.php");
  
  class Database {
  
      protected $db;
      protected $pdo;
  
      function __construct(){
          $this-&gt;db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
          if($this-&gt;db-&gt;connect_errno &gt; 0){
              die('Unable to connect to database [' . $this-&gt;db-&gt;connect_error . ']');
          }
  
          $this-&gt;pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS );
          $this-&gt;pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $this-&gt;pdo-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      }
  
      function __destruct(){
          $this-&gt;db-&gt;close();
          $this-&gt;pdo = null;
      }
  
  
          $query = 'INSERT INTO `'.DB_TEMPLATES.'` ( `'.DB_TEMPLATES_NAME.'` ) VALUES ( :name )';
  
          $stmt = $this-&gt;pdo-&gt;prepare($query);
          $stmt-&gt;bindValue(":name", $name, PDO::PARAM_INT);
          $stmt-&gt;execute();
  
      }
  
      function updateTemplate($id, $name, $value, $major ){
  
          if( $major == "major" ){ $major = 1; }
          else{ $major = 0; }
  
          $query = 'UPDATE `'.DB_TEMPLATES.'` 
                      SET 
                      `'.DB_TEMPLATES_NAME.'`=:name,
                      `'.DB_TEMPLATES_VALUE.'`=:value,
                      `'.DB_TEMPLATES_MAJOR.'`=:major
                      WHERE
                      `'.DB_TEMPLATES_ID.'`=:id';
  
          $stmt = $this-&gt;pdo-&gt;prepare($query);
          $stmt-&gt;bindValue(":name", $name, PDO::PARAM_STR );
          $stmt-&gt;bindValue(":value", $value, PDO::PARAM_STR );
          $stmt-&gt;bindValue(":major", $major, PDO::PARAM_INT );
          $stmt-&gt;bindValue(":id", $id, PDO::PARAM_INT );
  
          $stmt-&gt;execute();
  
      }
  
  
          $query = '  DELETE FROM '.DB_TEMPLATES.'
                      WHERE
                      '.DB_TEMPLATES_ID.'=:id';
  
          $stmt = $this-&gt;pdo-&gt;prepare($query);
          $stmt-&gt;bindValue(":id", $id, PDO::PARAM_INT);
  
          $stmt-&gt;execute();
  
  
      }
  
      (snip - irrelevant functions)
  
  }
  </code></pre>
  
  <p>At certain points along the way, I had thought this may have been a character encoding issue, but I don't think that any more. Perhaps it is some odd character escaping issue? Other posts here on SO seem to indicate it is a configuration issue / (feature?) of apache's mod_security. I tried disabling a few different things but that did not work, and now I am just at a loss as for what to think.</p>
  
  <p>Thanks for your help.</p>