title: Cordova AJAX POST permission
body: <p>I'm building a Cordova APP with simple REST calls.</p>
  
  <p>The issue is when I make a AJAX with POST, Chrome sends me: <strong>"XMLHttpRequest cannot load <a href="http://192.168.1.111/project/app">http://192.168.1.111/project/app</a>. Response for preflight has invalid HTTP status code 405"</strong> on console.</p>
  
  <p>But, if I make a AJAX call with GET (basically return an value from database) the things works like a charm.</p>
  
  <p>My AJAX call is:</p>
  
  <pre><code>$.ajax({
    url: "http://192.168.1.111/project/app",
    type: "POST",
    dataType: "json",
    contentType: 'application/json',
    data: {
      "name": "Cordova"
    },
    success: function() {
      navigator.notification.alert("Success!");
    },
    error: function(jqXHR, textStatus, errorThrown) {
      console.log(textStatus + jqXHR.responseText);
    }
  });
  </code></pre>
  
  <p><strong>POST handler in REST</strong></p>
  
  <pre><code>require 'Slim/Slim.php';
  $app = new Slim();
  $app-&gt;post('/app', 'addApp');
  $app-&gt;run();
  
  function addApp() {
      error_log('addApp\n', 3, '/var/tmp/php.log');
      $request = Slim::getInstance()-&gt;request();
      $callback = json_decode($request-&gt;getBody());
      $sql = "INSERT INTO app (name) VALUES (:name)";
      try {
          $db = getConnection();
          $stmt = $db-&gt;prepare($sql);  
          $stmt-&gt;bindParam("name", $callback-&gt;name);
          $stmt-&gt;execute();
          $callback-&gt;id = $db-&gt;lastInsertId();
          $db = null;
          echo json_encode($callback); 
      } catch(PDOException $e) {
          error_log($e-&gt;getMessage(), 3, '/var/tmp/php.log');
          echo '{"error":{"text":'. $e-&gt;getMessage() .'}}'; 
      }
  }
  
  function getConnection() {
      $dbhost="localhost";
      $dbuser="myuser";
      $dbpass="mypass";
      $dbname="mydb";
      $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
      $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      return $dbh;
  }
  </code></pre>
  
  <p><strong>What i've tried:</strong></p>
  
  <p><strong>1#</strong>: Access origin in config.xml</p>
  
  <pre><code>&lt;access origin="*" /&gt;
  </code></pre>
  
  <p><strong>2#</strong>: Add the connect-src MY IP to meta tag</p>
  
  <pre><code>&lt;meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src 'self' http://192.168.1.111"&gt;
  </code></pre>
  
  <p><strong>3#</strong>: Add Access-Control-Allow-Origin in my REST app .htaccess</p>
  
  <pre><code>Header add Access-Control-Allow-Origin "*"
  Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
  </code></pre>
  
  <p><strong>4#</strong>: Add support cors in JavaScript</p>
  
  <pre><code>$.support.cors = true;
  $.mobile.allowCrossDomainPages = true;
  </code></pre>
  
  <p><strong>5#</strong>: To check if is not error in Server POST, I used CURL and works normal (success)</p>
  
  <pre><code>curl -i -X POST -H 'Content-Type: application/json' -d '{"name": "Cordova"}' http://192.168.1.111/project/app
  </code></pre>
  
  <p><strong>OBS:</strong> I'm using Slim framework to REST (<a href="http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/">http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/</a>)</p>
  
  <p>================</p>
  
  <p><strong>Update #1:</strong></p>
  
  <p>I've upload my database and application to an online host and the results still the same.</p>
  
  <p>================</p>
  
  <p><strong>Update #2:</strong></p>
  
  <p>Doing another tests, I noticed that removing the <strong>contentType: "application/json"</strong> the debugger shows another error.</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be empty
  </code></pre>
  
  <p>================</p>
  
  <p>It's been two days that I'm trying to resolve this and nothing appears as a solution. I'm really sad.</p>